0

I have a simple checkbox on posts to display posts in a slider, and everything works fine. But I want to switch and add only pages in the slider, but code below is not updating wp_postmeta table of the pages, only posts. Could you please direct me what am I doing wrong

function damad_add_post_metabox() {

$screens = array( 'post', 'page');

foreach ( $screens as $screen ) {

    add_meta_box(
        'damad_post_meta_id',
        __( 'Options', 'damad' ),
        'damad_inner_custom_box',
        $screen
    );
}
}
add_action( 'add_meta_boxes', 'damad_add_post_metabox' );

function damad_inner_custom_box( $post ) {

wp_nonce_field( 'damad_inner_custom_box', 'damad_inner_custom_box_nonce' );

  $homepage = get_post_meta( $post->ID, '_post_featured', true);
  echo '<label for="post-featured">';
       _e("Include in homepage slider", 'damad');
  echo "</label>";
  echo '<input type="checkbox" id="post-featured" name="post-featured" value="post-featured"';
  if ( 'post-featured' == $homepage ) {
    echo 'checked="checked"';
  } 
  echo '/>';
}

 function damad_save_postdata( $post_id ) {

  if ( ! isset( $_POST['damad_inner_custom_box_nonce'] ) )
   return $post_id;

  $nonce = $_POST['damad_inner_custom_box_nonce'];

  if ( ! wp_verify_nonce( $nonce, 'damad_inner_custom_box' ) )
     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;

 } else {

   if ( ! current_user_can( 'edit_post', $post_id ) )
    return $post_id;
 }

$featureddata = $_POST['post-featured'];

  update_post_meta( $post_id, '_post_featured', $featureddata );
}
add_action( 'save_post', 'damad_save_postdata' );
5
  • Try and debug it, remove all the conditions from the damad_save_postdata() except if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id; and see if this works and if it does then put the conditions one by one and get hold of the culprit :) Commented Dec 12, 2013 at 10:43
  • @MarutiMohanty thanks, in the mean time I realised that any sort of meta data on pages is not saved, tried adding fields with advanced custom field plugin, and it is not saved either, so problem lays somewhere else... which is a bad news :( Commented Dec 12, 2013 at 11:09
  • 1
    ok change back to any default theme in case you are not and check the same ... if it works then its the theme you need to look into Commented Dec 12, 2013 at 11:27
  • @MarutiMohanty actually what I realised is that only meta fields are being updated on only one page, others are not picking it up, no matter if i use plugin or code above... any thoughts what could cause that? Commented Dec 13, 2013 at 13:00
  • Check for something such in your active theme's functions.php file. Commented Dec 13, 2013 at 14:09

1 Answer 1

0

change the line

if ( 'page' == $_POST['post_type'] ) {

To

if ( 'page' == get_post_type($post_id) ) {
1
  • that didn't do it, that if statement is there to check if the user has permission to perform the save. Commented Dec 11, 2013 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.