How do I set all of a particular post meta to a value within the custom post type I’m in?

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’ );
}

Read more here:: How do I set all of a particular post meta to a value within the custom post type I’m in?

Leave a Reply

Your email address will not be published. Required fields are marked *