Andrea G. Pigliafreddo
Andrea G. Pigliafreddo

Reputation: 347

WordPress: WP_Query with page_id AND meta_query

I have a problem with WP_Query:

I need get a list of a custom post types with a custom meta key scuola_data_approved=1 OR post_id=1208

I have tried to manipulate the following query but I can not find a working solution:

$args = array
(
'post_type'  => 'scuole', 
'posts_per_page' => -1,
'relation' => 'OR',
'page_id'=>1208,
'meta_query' => array
( 
    'relation' => 'OR',
    array(
       'key'     => 'scuola_data_approved',
       'value'   => '1',
       'compare' => '='
    ),

),

);

Upvotes: 0

Views: 856

Answers (1)

Blackbam
Blackbam

Reputation: 19376

This will not work you are fundamentally misunderstanding WP_Query. If you are using the page_id parameter you will automatically limit the results to one single post. The post with ID 1208. And this post will only be retrieved if it is of post_type page.

There is nothing like 'relation' => 'OR' for WP_Query - this is only possible in meta and tax queries. And there it is useless unless you have at least two different aspects you want to query.

In order to retrieve all posts as you describe it in your question do the following:

$args1 = array
(
'post_type'  => 'scuole', 
'posts_per_page' => -1,
'meta_query' => array
( 
    array(
       'key'     => 'scuola_data_approved',
       'value'   => 1,
       'type'    => 'numeric',
       'compare' => '='
    )
)
);

$all_posts_i_need = array_merge(get_posts($args1),array(get_post(1208));

Further information here: https://codex.wordpress.org/Class_Reference/WP_Query

Upvotes: 1

Related Questions