I used WCK Pro to create a post type ‘distributors’. Then to create a custom metabox that’s a repeater with metadata id ‘orderdetails’. It has 3 fields and as many rows as you want.
If I enter data into the repeater via the edit post screen, it enters just fine and you can see the data in the template on the front end.
There is also a way for distributors (as a user type) can log in and from the front end submit an order using a custom form. That custom form, upon submit sends a couple of emails, builds the form fields into an array and then uses update_post_meta to upload that array to the meta box (e.g. update_post_meta(get_the_ID(),’orderdetails’,$orderArray); )
This works just fine as long as NO data was ever entered through the back end system (e.g. the WCK interface on the post edit screen). However, if data was ever entered via the WCK interface on the post edit screen, programmatic updates to post meta stop working — and delete_post_meta also does not work for some reason, so it’s not like I can simply delete whatever’s there and replace it with the array that’s built in the template once the form is submitted.
I get this is a nuanced issue, but hoping maybe someone else has run into this problem and found a solution.
]]>I think it should be something like this, but I can’t manage (yet).
$post_args = array(
'post_type' => 'event',
'posts_per_page' => -1
);
$posts = get_posts( $post_args );
if ( $posts ) {
foreach ( $posts as $item ) {
// remove $item->_yoast_wpseo_title
// remove $item->custom->_yoast_wpseo_title
}
}
]]>I’m using the MH magazine lite theme that uses the featured images in a rather nice way. I’d like to customize this function though. I want to make the use of thumbnails optional and added – succesfully – added a meta box for that. I use a checkbox to set whether the featured image should be used, this should be not checked by default (no post meta set):
add_action('add_meta_boxes', 'et_add_meta_boxes');
add_action('save_post', 'et_save_meta_boxes', 10, 2 );
if (!function_exists('et_add_meta_boxes')) {
function et_add_meta_boxes() {
add_meta_box('et_post_details', __('Geen banner', 'mh'), 'et_post_meta', 'post', 'side', 'low');
}
}
if (!function_exists('et_post_meta')) {
function et_post_meta() {
global $post;
$custom = get_post_custom_values('show_thumb',$post->ID);
if (isset($custom[0]) && $custom[0] == 1){
$checked = ' CHECKED';
}
else { $checked = '';}
wp_nonce_field('et_meta_box_nonce', 'meta_box_nonce');
echo '<p>';
echo __("Gebruik uitgelichte foto boven artikel ", 'et');
echo '<input type="checkbox" name="show_thumb" id="eh-subheading" value="1" size="30"'.$checked.' />';
echo '</p>';
}
}
When the post is saved I want to delete or add the post meta (this depends on the value of the checkbox):
if (!function_exists('et_save_meta_boxes')) {
function et_save_meta_boxes($post_id, $post) {
GLOBAL $_POST;
if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'et_meta_box_nonce')) {
return $post->ID;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post->ID;
}
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post->ID;
}
}
elseif (!current_user_can('edit_post', $post_id)) {
return $post->ID;
}
if ('post' == $_POST['post_type']) {
$meta_data['et-subheading'] = esc_attr($_POST['et-subheading']);
}
$custom = get_post_custom_values('show_thumb',$post->ID);
if (isset($_POST['show_thumb'])){
add_post_meta($post->ID, 'show_thumb', '1');
}
elseif (empty($_POST['show_thumb'])){
delete_post_meta($post->ID,'show_thumb');
}
}
}
Somehow when I publish (or save) the post nothing happens. The checkbox goes back to the value that was set before publishing.
I’m not quiet sure what I’m doing wrong/where to start debugging.
Any thoughts?
Kind regards,
Niellles
P.s I based this on a meta box already added by the theme itself.
]]>From testing, I know that the delete_option and delete_post_meta calls are running, they just return FALSE. In particular, delete_option gets hung up here:
$row = $wpdb->get_row( $wpdb->prepare( “SELECT autoload FROM $wpdb->options WHERE option_name = %s”, $option ) );
if ( is_null( $row ) )
return false;
Adding and changing database rows works fine.
Does anyone have any idea why this would happen?
]]>However, I can’t delete them. Here’s the code I’m using:
[Code moderated as per the Forum Rules. Please use the pastebin]
Any help appreciated. Thanks.
Jake
I’ve added some custom metaboxes to my edit post page and I have the following problems:
1. Meta data not stored (was working)
2. Medta not deleted when empty (was not working wen 1. was working).
Is there something wrong with my code?
I got the following code in my functions.php file:
add_action( 'admin_menu', 'create_custom_meta_box' );
add_action( 'save_post', 'save_custom_post_meta_box', 10, 2 );
function create_custom_meta_box() {
add_meta_box( 'Intro', __('Intro'), 'post_intro_meta_box', 'post', 'normal', 'high' );
}
function save_custom_post_meta_box( $post_id, $post ) {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
// Custom intro text box (Ingress)
// If custom field does not exists, it is created.
// Then add, update and delete handles are added
/*--------------------------------------------------------------------------- */
if ( !wp_verify_nonce( $_POST['post_intro_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
return $post_id;
// Mini intro
$meta_value = get_post_meta( $post_id, '_post_mini_intro', true );
$new_meta_value = stripslashes( $_POST['post_mini_intro'] );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, '_post_mini_intro', $new_meta_value, true );
elseif ( $new_meta_value != $meta_value )
update_post_meta( $post_id, '_post_mini_intro', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, '_post_mini_intro', $meta_value );
}
]]>$allposts = get_posts('numberposts=0&post_type=post&post_status=');
foreach( $allposts as $postinfo) {
delete_post_meta($postinfo->ID, '_myPluginName');
}
But it only deletes the entries from the 5 most recent posts (the other data stays just in the database).
When I ‘prinr()’ the $allposts, I also get only the data from the 5 most recent post.
How can I fix it?
]]>Well my problem is I want to delete a key that says ‘Current Mood:’and I can’t seem to do it. Its just there I’m not using it in any posts. I just want to delete it cause its idle and useless. I’ve read the delete_post_meta page already, but now my problem is where do i put the codes?
I’m sorry if you are little annoyed. I’ve done my best to use google search engines and wordpress docs but i cant seem to have the right key words if there is a topic about it already.
Thanks for help!
]]>