我有一个WP_Query的问题:
我需要使用自定义元键scuola_data_approved=1 OR post_id=1208自定义帖子类型的列表scuola_data_approved=1 OR post_id=1208
我试图操纵以下查询,但我找不到一个有效的解决方案:
$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' => '=' ), ), );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' => '=' ), ), );最满意答案
这不起作用你从根本上误解了WP_Query 。 如果您使用的是page_id参数,则会自动将结果限制为一个帖子。 ID 1208的帖子。 只有在post_type页面中才会检索此帖子。
WP_Query没有像'relation' => 'OR'的东西 - 这只能在元查询和税务查询中使用。 除非你想要查询至少两个不同的方面,否则它是无用的。
为了检索您在问题中描述的所有帖子,请执行以下操作:
$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));更多信息请访问: https : //codex.wordpress.org/Class_Reference/WP_Query
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
更多推荐
发布评论