Is it possible to use not custom fields in get_posts()
's 'meta_query'
to use a or
-relation statement with a custom field and a category id?
Like this:
$args = array(
'posts_per_page' => 6
, 'offset' => 0
, 'meta_query' => array(
'relation' => 'OR'
, array(
'key' => 'placing'
, 'value' => 'nn'
)
, array(
'key' => 'category_id'
, 'value' => $cat->term_id
)
)
);
$myposts = get_posts($args);
And if so, should it be term_id
or category_id
?
Or how do I accomplish that?
Is it possible to use not custom fields in get_posts()
's 'meta_query'
to use a or
-relation statement with a custom field and a category id?
Like this:
$args = array(
'posts_per_page' => 6
, 'offset' => 0
, 'meta_query' => array(
'relation' => 'OR'
, array(
'key' => 'placing'
, 'value' => 'nn'
)
, array(
'key' => 'category_id'
, 'value' => $cat->term_id
)
)
);
$myposts = get_posts($args);
And if so, should it be term_id
or category_id
?
Or how do I accomplish that?
Share Improve this question asked Sep 25, 2015 at 13:02 Peter WesterlundPeter Westerlund 1,0775 gold badges14 silver badges31 bronze badges1 Answer
Reset to default 0No, it doesn't work like that because meta_query
is for custom fields and tax_query
is for taxonomies.
But what you could do is run two separate queries to get all of the IDs that qualify, and then use those IDs in a 3rd query with post__in
. For example:
$cat_query_args = array(
'posts_per_page' => -1,
'cat' => '63',
);
$meta_query_args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'placing',
'value' => 'nn',
)
)
);
$cat_query = get_posts( $cat_query_args );
$meta_query = get_posts( $meta_query_args );
$merged_posts = array_merge( $cat_query, $meta_query );
$combined_query_args = array(
'posts_per_page' => 6,
'offset' => 0,
'post__in' => $merged_posts
);
$combined_posts = get_posts( $combined_query_args );