Reputation: 92
I've got the below code to return a list of jobs that are marked as complete but don't have an invoice number inputted into the invoice_number text field yet. It returns nothing even though I've not inputted any invoice numbers yet.
$posts = get_posts(array(
'post_type' => 'jobs',
'posts_per_page' => -1,
'meta_key' => 'job_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'job_status',
'field' => 'slug',
'terms' => array( 'complete' )
),
),
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'invoice_number',
'value' => '',
'compare' => '='
)
)
));
Upvotes: 1
Views: 23
Reputation: 4534
In the meta_query
part you are explicitly looking for jobs with invoice_number
equal to ''
(empty string).
However, when a meta (field & value) is not assigned to a post (in your case a job), it will not exist at all, so you need to use:
...
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'invoice_number',
'compare' => 'NOT EXISTS'
)
)
...
Reference: https://developer.wordpress.org/reference/classes/wp_meta_query/
Also, usually singular form is used for post_type
values, so make sure that your current "jobs"
value is actually correct.
Upvotes: 1