1

I've currently got a custom checkbox that (if checked) sets a particular post as 'featured'. It also set all other posts as 'unfeatured', just before setting the current post.

This has worked great, but I now have multiple custom posts types and would like the 'unfeatured' part to only work locally to within the custom post type I'm in.

Does that make sense?

So if I'm editing a 'white paper' section and I check it as 'featured', it unsets any previously featured white papers, but leaves the other custom post types alone. Here's the code that unsets ALL featured posts from ALL post types:

if ( isset( $_POST[ 'is-it-featured' ] ) ) {
    $posts = get_posts($args);
    foreach ( $posts as $post ) {
        // Run a loop and update every meta data
        update_post_meta( $post->ID, 'is-it-featured', '0' );
    }
    update_post_meta( $post_id, 'is-it-featured', '1' );
} else {
    $posts = get_posts($args);
    update_post_meta( $post_id, 'is-it-featured', '0' );
}

1 Answer 1

0

Please include the $args parameter you use.

However, within the get_posts $args parameter, you can use the "post_type" arg to limit the found posts to a specific post type.

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.