-
-
Save sirjonathan/1b62f5fdc8e4005403e9 to your computer and use it in GitHub Desktop.
WP Alchemy Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First, here's the setup for the metabox. | |
$cr_brands_metabox = $cr_brands_data = new WPAlchemy_MetaBox(array | |
( | |
'id' => '_brands_custom_results', | |
'title' => 'Brands Custom Results Options', | |
'template' => get_stylesheet_directory() . '/metaboxes/brands-meta.php', | |
// Only show this metabox on the page with template-custom-results.php attached | |
'include_template' => array('tpl-cr-brands.php'), | |
'mode' => WPALCHEMY_MODE_EXTRACT | |
)); | |
// Now, here's relevant part of the brands-meta.php | |
<p><label>Brand Name</label> | |
<?php $mb->the_field('listing_brand_ac'); ?> | |
<select name="<?php $mb->the_name(); ?>"> | |
<option value="">Select...</option> | |
<?php foreach($unique_brands as $brand) { ?> | |
<option value="<?php echo $brand; ?>"<?php $mb->the_select_state($brand); ?>><?php echo $brand; ?></option> | |
<?php } ?> | |
</select> | |
</p> | |
// It works great. When you edit a page with the metabox assigned, you select from the drop down, save, and it shows up in the "meta" within the $cr_brands_data variable. | |
// Now, here is the code for the function that I use to create pages and populate the same field, "listing_brand_ac". | |
function dh_create_child_pages() { | |
global $wpdb; | |
$wpdb->show_errors(); | |
// First, decide what's going to drive pages - What is the keyword source? | |
$keywords_data = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix ."cp_ad_fields WHERE field_name = 'cp_make' "); | |
$keywords_array = explode(",", $keywords_data->field_values); | |
// $keywords_array = array("Test #5"); | |
$parent_page_id = "266"; | |
$page_tempate_name = "tpl-cr-brands.php"; | |
foreach( $keywords_array as $keyword ) { | |
$new_page = array( | |
'post_title' => $keyword . ' New & Used Air Compressors', | |
'post_name' => $keyword, // Slug | |
'post_content' => '', | |
'post_status' => 'publish', | |
'post_parent' => $parent_page_id, | |
'post_type' => 'page' | |
); | |
// Insert the page | |
$new_page_id = wp_insert_post($new_page); | |
// Assign the correct page template | |
update_post_meta($new_page_id, '_wp_page_template', $page_tempate_name); | |
// Now, insert the value for WP Alchemy | |
add_post_meta($new_page_id, 'listing_brand_ac', $keyword); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment