Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
share|improve this question
    
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 :) –  Maruti Mohanty Dec 12 '13 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 :( –  Bojana Šekeljić Dec 12 '13 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 –  Maruti Mohanty Dec 12 '13 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? –  Bojana Šekeljić Dec 13 '13 at 13:00
    
Check for something such in your active theme's functions.php file. –  Maruti Mohanty Dec 13 '13 at 14:09

1 Answer 1

change the line

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

To

if ( 'page' == get_post_type($post_id) ) {
share|improve this answer
    
that didn't do it, that if statement is there to check if the user has permission to perform the save. –  Bojana Šekeljić Dec 11 '13 at 21:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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