WordPress has an extremely powerful set of tools for finding and displaying your content, no matter what parameters or custom post types you’re searching in.
The WP_Query class allows you to search for posts based on different parameters – such as author, date, post type, or any custom fields – which can be passed as an array when initializing the class.
You can make incredibly complex and useful queries checking for a specific value in your custom fields, but what happens when some of your posts have no data for a given custom field?
Here’s what you need to know.
Let’s say we have a custom post type called “Board Member” with a custom checkbox field whose label is Past Board Member.
Presumably, in your organization’s Board Member listing, you have both current and former board members displayed, and checking the Past Board Member box assigns of a value of 1 to show that the person is no longer a board member at your organization. The Past Board Member field has a name attribute of past_board_member.
To pull up a list of all Board Members members where the Past Board Member checkbox is checked, we would use the following:
$args = array(
'post_type' => 'board-member',
'meta_query' => array(
array(
'key' => 'past_board_member',
'value' => 1,
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
In this code, we’re looking for Board Member posts and using the meta_query parameter to find entries where the value of the custom field past_board_member has a value equal to 1.
Easy enough, right?
In this example, imagine you have years of board member listings in the back-end of your site, and it’s possible not all of them were individually updated with a value for the Past Board Member custom field.
So while our query will show every person who has been assigned the Past Board Member value of 1, we won’t show any listings where the field has yet to be updated.
In this case, we need to extend the meta_query so that it looks for all Board Members who either have a custom field with a key of past_board_member and a value of 1 or does not have the custom field at all.
$args = array(
'post_type' => 'board-member',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'past_board_member',
'value' => 1,
'compare' => '=',
),
array(
'key' => 'past_board_member',
'compare' => 'NOT EXISTS',
),
),
);
$query = new WP_Query( $args );
In this code, we add the relation
parameter inside the meta_query parameter with a value of OR
. This tells the WP_Query class the relationship between each array of parameters inside the meta_query parameter. We also add an array to the meta_query parameter with a looking for the past_board_member field, but instead of the value being 1, we are using a value of NOT EXISTS.
This NOT EXISTS value tells the WP_Query class to look for all Board Member posts where the past_board_member field equals 1 OR it doesn’t exist at all.
By altering the query with a few new parameters, we can get it to pull posts based on the arguments passed to it. If you need help with your WordPress site or for any other digital marketing needs, reach out to us.