How to show the post which checkbox is not selected?
I don’t know what the meta value is set to when the checkbox is not checked (or maybe no meta set at all?), but you can use the meta_query
argument for searching for posts where the meta is not set (not yet added to the post) or that the value is empty, e.g. in your case, “empty” could mean the meta value is a 0
(zero) or nothing (i.e. an empty string).
Hence in the following example, I used 'value' => [ 0, '' ]
which then automatically sets the compare
to IN
because we’re passing an array of values.
$args = array(
// your other args here, but without the meta_key and meta_value
'meta_query' => array(
'relation' => 'OR',
// Search for posts where the meta does not exist.
array(
'key' => 'isfeatured',
'compare' => 'NOT EXISTS',
),
// Search for posts where the meta value is empty.
array(
'key' => 'isfeatured',
'value' => [ 0, '' ],
),
),
);
And here’s an example if you want to use meta_query
to retrieve posts where your checkbox is checked (and whereby the meta value is 1):
$args = array(
// your other args here, but without the meta_key and meta_value
'meta_query' => array(
// Search for posts where the meta value is exactly 1 (one).
array(
'key' => 'isfeatured',
'value' => 1,
),
),
);
Check this out for more details about meta query parameters in WP_Query
.
Additional Notes
-
There’s a mistake in your code — You need to switch the position of
return $data;
andwp_reset_postdata();
, i.e. callwp_reset_postdata()
first. -
Shortcode should always return something, just as with filter hooks like
the_content
, so I’d addreturn '';
at the end of your shortcode function. 🙂 -
Meta queries like the one above are known to cause performance issues when the site gets bigger (many posts, users, plugins, options, visitors, etc.), so as I said in my comment, you should consider using a taxonomy/term instead to mark posts that you want to be “featured”.
After all, taxonomy was meant to be used for grouping things together, and taxonomy terms don’t have to be titles such as
Featured
orFoo Bar
and instead, they can be0
or1
to represent a boolean value.