WordPress:带有page

编程入门 行业动态 更新时间:2024-10-20 00:26:34
WordPress:带有page_id和meta_query的WP_Query(WordPress: WP_Query with page_id AND meta_query)

我有一个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

更多推荐

本文发布于:2023-07-06 08:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WordPress   page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!