0

I have created a meta box for a theme I am working on, it is a simple meta box with a check box for featured post. The problem is user can tick that check box for multiple posts in the same category.

I want to disable the meta value from an old post once a new post published under that category. So basically only allow 1 post to be checked as featured on any given category. I think I should do something with

delete_post_meta( $post_id, 'featured_post' );

but I am not sure how can I check previous posts and delete the post meta from them. Any suggestion?

Update #1 :

This is my meta box:

//FEATURED POST CHECK BOX

//ADD THE META BOX
add_action( 'add_meta_boxes', 'add_featured_slide' );
function add_featured_slide(){
    //POST TYPES TO HAVE THE CUSTOM META BOX 
    $ctptypes = array( 'post', 'page', 'your_custom_post_type' );
    foreach ( $ctptypes as $ctptype ) {
        add_meta_box( 'featured-slide', 'Featured Post', 'featured_slide_func', $ctptype, 'side', 'high' );
    }
}
//DEFINE THE META BOX
function featured_slide_func( $post ){
    $values = get_post_custom( $post->ID );
    $check = isset( $values['special_box_check'] ) ? esc_attr( $values['special_box_check'][0] ) : '';
    wp_nonce_field( 'my_featured_slide_nonce', 'featured_slide_nonce' );
    ?>
    <p>
        <input type="checkbox" name="special_box_check" id="special_box_check" <?php checked( $check, 'on' ); ?> />
        <label for="special_box_check">Feature this post?</label>
    </p>
    <?php 
}
//SAVE THE META BOX DATA WITH THE POST
add_action( 'save_post', 'featured_slide_save' );
function featured_slide_save( $post_id ){
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if( !isset( $_POST['featured_slide_nonce'] ) || !wp_verify_nonce( $_POST['featured_slide_nonce'], 'my_featured_slide_nonce' ) ) return;
    if( !current_user_can( 'edit_post' ) ) return;
    $allowed = array( 
        'a' => array( 
            'href' => array() 
        )
    );
    // IF CHECKED SAVE THE CUSTOM META
    if ( isset( $_POST['special_box_check'] ) && $_POST['special_box_check'] ) {
        add_post_meta( $post_id, 'special_box_check', 'on', true );
    }
    // IF UNCHECKED DELETE THE CUSTOM META
    else {
        delete_post_meta( $post_id, 'special_box_check' );
    }
}
  • You should show us the code to display the meta box. BTW, a featured post is a sticky post, and it's a native function in WordPress. Why don't you use it? You can style the .sticky class if you are using the post_class() function to generate your post classes, which is the best practice. – Gerard Oct 31 '15 at 3:06
  • @Gerard I updated the question – Rain Man Oct 31 '15 at 4:37
0

I guess you want to delete all meta values from all posts with this:

delete_metadata('post', null, $meta_key, null, true)

See the documentation which says, that the last argument is ment to delete all entries from all objects:

delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false)

$delete_all (boolean) (optional) Optional, default is false. If true, delete matching metadata entries for all objects, ignoring the specified object_id. Otherwise, only delete matching metadata entries for the specified object_id. Default: false

https://codex.wordpress.org/Function_Reference/delete_metadata

Afterwards you can use wp_get_recent_posts() to get the latest post from a category and assign the meta value there.

https://codex.wordpress.org/Function_Reference/wp_get_recent_posts

  • yes, but how can I make sure that only the last post in the given category has that meta? – Rain Man Oct 31 '15 at 20:28
  • your description is obscure in that point. so I added how you get the recent post to my answer – LeMike Nov 1 '15 at 2:27
0

You claim that you want to "..allow 1 post to be checked as featured on any given category." To do that you will need to associate your special_box_check with the categories rather than have a simple on/off toggle. That requires a change in the form where $check is set and checked() is used. It also requires a change to how the data is saved. Look for "Changes follow" in the source below.

//ADD THE META BOX
add_action( 'add_meta_boxes', 'add_featured_slide' );
function add_featured_slide(){
    //POST TYPES TO HAVE THE CUSTOM META BOX 
    $ctptypes = array( 'post', 'page', 'your_custom_post_type' );
    foreach ( $ctptypes as $ctptype ) {
        add_meta_box( 'featured-slide', 'Featured Post', 'featured_slide_func', $ctptype, 'side', 'high' );
    }
}
//DEFINE THE META BOX
function featured_slide_func( $post ){
    $values = get_post_custom( $post->ID );
    $check = isset( $values['special_box_check'] ) ? true : false;
    wp_nonce_field( 'my_featured_slide_nonce', 'featured_slide_nonce' );
    ?>
    <p>
        <input type="checkbox" name="special_box_check" id="special_box_check" <?php checked( $check); ?> />
        <label for="special_box_check">Feature this post?</label>
    </p>
    <?php 
}
//SAVE THE META BOX DATA WITH THE POST
add_action( 'save_post', 'featured_slide_save', 10, 2 );
function featured_slide_save( $post_id, $post ){
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if( !isset( $_POST['featured_slide_nonce'] ) || !wp_verify_nonce( $_POST['featured_slide_nonce'], 'my_featured_slide_nonce' ) ) return;
    if( !current_user_can( 'edit_post' ) ) return;
    $allowed = array( 
        'a' => array( 
            'href' => array() 
        )
    );

    // Changes follow
    if ( isset( $_POST['special_box_check'] ) && $_POST['special_box_check'] ) {
        $cats = wp_get_post_categories($post_id);
        foreach ($cats as $v) {
          delete_metadata ( 'post', null, 'special_box_check', $v, true );
        }
        foreach ($cats as $v) {
          add_post_meta( $post_id, 'special_box_check', $v);
        }
    } else {
        delete_post_meta( $post_id, 'special_box_check' );
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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