admin管理员组文章数量:1301548
I have 2 WP_queries that I want to combine into 1 query. These are my orginal 2 queries:
$childlist = new WP_Query( array (
'numberposts' => -1,
'post_type' => 'page',
'post_parent' => $post->ID,
'fields' => 'ids'
));
$childlist2 = new WP_Query( array (
'numberposts' => -1,
'post_type' => 'page',
'meta_key' => 'extra_menu',
'meta_value' => $post->ID,
'meta_compare' => 'LIKE',
'fields' => 'ids'
));
I tried combining them in several differten ways, but I can't seem to find a working solution. As soon as I try this OR relation it will only give results for the second part in the "OR".
$childlist = new WP_Query(array(
'numberposts' => -1,
'post_type' => 'page',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'post_parent',
'value' => $post->ID,
'compare' => '=',
),
array(
'key' => 'extra_menu',
'value' => $post->ID,
'compare' => 'LIKE',
),
),
));
Any thoughts on how I can fix this?
I have 2 WP_queries that I want to combine into 1 query. These are my orginal 2 queries:
$childlist = new WP_Query( array (
'numberposts' => -1,
'post_type' => 'page',
'post_parent' => $post->ID,
'fields' => 'ids'
));
$childlist2 = new WP_Query( array (
'numberposts' => -1,
'post_type' => 'page',
'meta_key' => 'extra_menu',
'meta_value' => $post->ID,
'meta_compare' => 'LIKE',
'fields' => 'ids'
));
I tried combining them in several differten ways, but I can't seem to find a working solution. As soon as I try this OR relation it will only give results for the second part in the "OR".
$childlist = new WP_Query(array(
'numberposts' => -1,
'post_type' => 'page',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'post_parent',
'value' => $post->ID,
'compare' => '=',
),
array(
'key' => 'extra_menu',
'value' => $post->ID,
'compare' => 'LIKE',
),
),
));
Any thoughts on how I can fix this?
Share Improve this question asked Mar 16, 2021 at 9:59 Rob BeirensRob Beirens 31 bronze badge 01 Answer
Reset to default 1meta_query
can only be used for meta queries, you can't use it for non-meta parameters. Even if you could, it would not give a performance benefit.
What you want can't be done, and your second example is searching for posts that have a post meta named post_parent
.
The canonical answer, is to use 2 query objects and 2 loops, or, to use a common method of indicating parent/child relationships so that only 1 query is necessary.
Additionally:
- Using
-1
to fetch all posts is dangerous, never do this. This is a great way to introduce memory exhaustion problems as the site grows larger- "But I only have 50 posts it should never get that far" then ask for 60 posts and guarantee that it never will. A high value is always preferable to unlimited value. If you need to show all of them, then infinite scrolling and pagination are far better options.
- By asking only for the IDs, you might think you are improving performance, but instead you are hurting it. WP will fetch all the post data in one query to speed things up, but now it has to do a query every, single, time to fetch individual post objects. So rather than 1 query to fetch 300 posts all at once, you now have 300 queries to fetch individual posts. Anytime you ask for the title, or post meta, grab the posts permalink, etc
- Querying for posts by their post meta values is super expensive, and gets more expensive as the site grows larger. This is what taxonomies are for, use a taxonomy. Think about it, why did they store post tags and categories in a taxonomy table instead of using post meta? Because it's slow! Post meta tables are optimised for finding values when you already know the post ID. Taxonomy tables are optimised for finding post IDs when you know the value
本文标签: wp queryCombine 2 WPqueries into one (OR relation)
版权声明:本文标题:wp query - Combine 2 WP_queries into one (OR relation) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741677644a2391978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论