admin管理员组文章数量:1314216
I want fetch all posts from the post type book
in the custom taxonomy book_tags
with the meta key lang
and the meta value en
.
My code:
<?php
$args = array(
'post_type' => 'book',
'posts_per_page' => 40,
'paged' => "$paged",
'meta_query' => array(
array(
'key' => 'lang',
'value' => 'en',
'compare' => 'LIKE'
)
),
'tax_query' => array(
array(
'taxonomy' => 'book_tags',
'field' => 'slug',
'terms' => get_queried_object()->slug
)
)
);
$additional_loop = new WP_Query($args);
while ($additional_loop->have_posts()) :
$additional_loop->the_post();
This doesn’t work? Why?
If I remove "meta_query" it works. Is there a bug in "meta_query"?
I want fetch all posts from the post type book
in the custom taxonomy book_tags
with the meta key lang
and the meta value en
.
My code:
<?php
$args = array(
'post_type' => 'book',
'posts_per_page' => 40,
'paged' => "$paged",
'meta_query' => array(
array(
'key' => 'lang',
'value' => 'en',
'compare' => 'LIKE'
)
),
'tax_query' => array(
array(
'taxonomy' => 'book_tags',
'field' => 'slug',
'terms' => get_queried_object()->slug
)
)
);
$additional_loop = new WP_Query($args);
while ($additional_loop->have_posts()) :
$additional_loop->the_post();
This doesn’t work? Why?
If I remove "meta_query" it works. Is there a bug in "meta_query"?
Share Improve this question edited Aug 10, 2013 at 7:34 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jul 22, 2013 at 3:37 kytdesignerkytdesigner 191 silver badge6 bronze badges 4 |1 Answer
Reset to default 1Since in your comment, you said that you just want posts with meta key lang=en
on a custom taxonomy page, the easiest way to do that would be to filter the query with pre_get_posts
before it is run.
function wpa_107371_meta_query( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
// only change the query on a custom taxonomy
// can check for a specific taxonomy if desired
if ( is_tax() ) {
//define our meta query
$meta_query = array(
array( // needs this nested array syntax to work
'key' => 'lang',
'value' => 'en',
'compare'=> '=',
),
);
$query->set('meta_query', $meta_query);
return;
}
}
add_action( 'pre_get_posts', 'wpa_107371_meta_query' );
本文标签: Query for custom post type objects in a taxonomy and with a meta value
版权声明:本文标题:Query for custom post type objects in a taxonomy and with a meta value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741932067a2405634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pre_get_posts
is a very common solution that people overlook when trying to modify a loop. Though for a start, I'd guess that"$paged"
is wrong (since the variable is in quotes) as might also beget_queried_object()->slug
. – helgatheviking Commented Jul 22, 2013 at 3:48meta_query
. Please edit your question to provide more details about what you are trying to do.. and where. – helgatheviking Commented Jul 22, 2013 at 14:00