WP get posts with no custom field value - wordpress

I have the following code:
$args = array(
'post_type' => 'epost',
'post_status' => 'future,publish',
'meta_key' => 'custorder',
'orderby'=>'meta_value',
'order' => 'asc',
'posts_per_page' => 900
);
But I can't get the posts on which the custorder value is not set. How can I retrieve those posts also?

You can do this in one query with a proper meta_query
$args = [
'post_type' => 'epost',
'post_status' => ['future','publish'],
'meta_key' => 'custorder',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 900,
'meta_query' => [
[
'key' => 'custorder',
'compare' => 'EXIST'
],
[
'key' => 'custorder',
'compare' => 'NOT EXISTS'
]
]
];

I don't know if is the best solution but I've found a workaround
$query1 = new WP_Query($args1);
$query2 = new WP_Query($args2);
$the_query = new WP_Query();
$the_query->posts = array_merge( $query1->posts, $query2->posts );
$the_query->post_count = $query1->post_count + $query2->post_count;
to match two different arguments into one query. For the first set of arguments I've used 'meta_key'=>'custorder' and for the second one I've added 'meta_compare'=>'NOT EXISTS'

So to find posts of type epost which does not have a meta field with key custorder assigned:
$metaKey = 'custorder';
$postType = 'epost';
$args = [
'post_type' => $postType,
'meta_query' => [
[
'key' => $metaKey,
'compare' => 'NOT EXISTS'
]
]
];
$query = new WP_Query( $args );

Try something like this:
'meta_query' => array(
array(
'key' => 'custorder',
'value' => '',
'compare' => '='
)
),
Source: https://codex.wordpress.org/Class_Reference/WP_Meta_Query

Related

Wordpress – post_per_page ignored on custom post type

following code is returning arbitrary 5 posts:
$args = array(
'post_type' => 'convenzioni',
'post_per_page' => 10,
'orderby' => 'post_title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'pinned',
'value' => 1,
'compare' => '=',
),
),
);
$the_query = get_posts( $args );
while it should return an higher number:
following code, confirms there are more and infact returns all the custom posts
$args = array(
'post_type' => 'convenzioni',
'post_per_page' => -1,
'orderby' => 'post_title',
'order' => 'ASC',
'nopaging' => true, //THANKS TO THIS!
'meta_query' => array(
array(
'key' => 'pinned',
'value' => 1,
'compare' => '=',
),
),
);
$the_query = get_posts( $args );
what am I missing?
ty

Wordpress ACF field not working in WP_Query

Why does this return all posts;
$partner = get_query_var('partner');
echo "Partner ID: ". $partner; // Echoes correctly
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => 'LIKE', // Also 'IN'
),
);
$partner_query = new WP_Query($args);
the ACF field 'partner' is an array, the query variable is a string (obv)
The correct usage of meta_query is like this:
The args shouldn't be meta_key if you're using meta_query - they should be key, value, etc.
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner',
'value' => array( $partner ),
'compare' => 'IN',
),
),
);
Try like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => '=', // Also 'IN'
),
);
$partner_query = new WP_Query($args);
For anyone who gets stuck in this rabbit hole.
My query parameter is a string, ACF stores the IDs as a string if there's one and an array if there's more than one.
This form of the arguments returns the correct results. (many thanks to Howard E and IronCanTaco)
$partner = get_query_var('partner'); // string
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner', // Could be string or array
'value' => $partner,
'compare' => 'LIKE',
),
),
);

ACF - don't show passed events and filter with start date

I have a list of events and I'd like to do 2 things :
order posts according to starting date
don't include passed events in the query
I did some research and builded a query but it doesn't work.
'''
$current_date = date_i18n('d.m.y');
$the_query = new WP_Query( array(
'post_type' => 'spectacles',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'type' => 'DATE'
),
array(
'key' => 'header_spec_period_end',
'value' => '$current_date',
'compare' => '>',
'type' => 'DATE'
),
),
'order' => 'ASC' ) );
?>
'''
All the events are showing, I can't get the passed events filtered.
$date_now = date('Ymd');
$args = array(
'post_type' => 'spectacles',
'post_status' => 'publish',
'meta_key' => 'header_spec_period_start',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'compare' => '>',
'value' => $date_now,
),
),
);
$the_query = new WP_Query( $args );
You can try this ...

List child pages only if specific custom field is not empty in those pages

So I have this function to list all child pages and I want it to don't list those elements which does not have custom field "role" or if this custom field is empty. I've tried different things but even if I type value or meta value "test" all pages are listed. There's something wrong with this query meta_key is working perfectly but meta_value doesn't work.
My function code is this:
function list_child_pages() {
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_key' => 'role',
'meta_value' => 'test',
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) {
return list_childs();
} else
$string="team members were not found";
return $string;
}
'meta_query' => [
'relation' => 'AND',
[
'key' => 'role',
'value' => 'test',
'compare' => '='
]
],
I prefer to use [] instead of array()
But you can also have
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'role',
'value' => 'test',
'compare' => '='
)
),
Integration with your example:
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'role',
'value' => 'test',
'compare' => '='
)
),
);
Try to read the documentation of the Class WP_Meta_Query at: https://codex.wordpress.org/Class_Reference/WP_Meta_Query#Initializing_WP_Meta_Query

How to use custom fields in wp_query for ordering and filtering the result at the same time?

I want to query the posts that have been set as "featured" and order them by their "priority" field at the same time.
featured field is a true/false type and priority field is a number, and they are created by ACF plugin.
here is my code, but it's not working...
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num date',
'meta_key' => 'priority',
'meta_query' => array(
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
try this :
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num',
'order'=>'DESC',
'meta_key' => 'priority',
'meta_query' => array(
relation=>'AND',
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
array(
'key' => 'priority',
'value' => array(1,6), //YOUR VALUES
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
),
);
$query = new WP_Query( $args );

Resources