admin管理员组文章数量:1130349
I am using pre_get_posts() to allow all posts to be displayed on any category archive. This is because I will provide a javascript sorting and filtering method using isotope.js. Any category page will output all posts, but any that aren't a part of that category will initially be hidden.
function show_all_cats( $query ) {
if ( !$query->is_main_query() ){
return;
}
if ( is_admin() ){
return;
}
if ( $query->is_archive ) {
$query->set('cat', ''); //here is the problem
var_dump($query);
return;
}
return;
}
add_action( 'pre_get_posts', 'show_all_cats' );
I have tried setting 'cat' to 0, null, '' and '-' . $currentcategoryid.
They all either display only posts that would be displayed otherwise (all in category) or none.
I tried using query_posts() which also didn't work. I was also told:
manipulating taxonomies might require re-running meta queries processing.
I am using pre_get_posts() to allow all posts to be displayed on any category archive. This is because I will provide a javascript sorting and filtering method using isotope.js. Any category page will output all posts, but any that aren't a part of that category will initially be hidden.
function show_all_cats( $query ) {
if ( !$query->is_main_query() ){
return;
}
if ( is_admin() ){
return;
}
if ( $query->is_archive ) {
$query->set('cat', ''); //here is the problem
var_dump($query);
return;
}
return;
}
add_action( 'pre_get_posts', 'show_all_cats' );
I have tried setting 'cat' to 0, null, '' and '-' . $currentcategoryid.
They all either display only posts that would be displayed otherwise (all in category) or none.
I tried using query_posts() which also didn't work. I was also told:
Share Improve this question edited Jan 29, 2014 at 13:41 kaiser 51k27 gold badges151 silver badges245 bronze badges asked Jan 29, 2014 at 13:24 BillBill 16510 bronze badgesmanipulating taxonomies might require re-running meta queries processing.
4 Answers
Reset to default 1Just unsetting the cat variable probably isn't enough. The pre_get_posts hook happens after the query variables have already been parsed. So there's probably a tax_query with the taxonomy = category and the terms = your category.
You're already dumping the $query in your code, presumably for debugging. So, look at what you're actually dumping. Do you see the tax_query? Change your code to adjust that $query to have what you want it to have.
Have you tried this?
unset( $query->query_vars['cat'] );
You can use a filter pre_get_posts() to modify SQL query WHERE part:
add_filter( 'posts_where', function ( $where ) {
$where = preg_replace("regex pattern", "", $where);
return $where;
}, 10, 2 );
It is a bit hacking solution but should work.
It is about 4 years later, but if anyone needs the answer, this is the solution:
if ( $query->is_archive ) {
$q->set( 'category_name', '*your-category-slug*' );
}
Cheers
I am using pre_get_posts() to allow all posts to be displayed on any category archive. This is because I will provide a javascript sorting and filtering method using isotope.js. Any category page will output all posts, but any that aren't a part of that category will initially be hidden.
function show_all_cats( $query ) {
if ( !$query->is_main_query() ){
return;
}
if ( is_admin() ){
return;
}
if ( $query->is_archive ) {
$query->set('cat', ''); //here is the problem
var_dump($query);
return;
}
return;
}
add_action( 'pre_get_posts', 'show_all_cats' );
I have tried setting 'cat' to 0, null, '' and '-' . $currentcategoryid.
They all either display only posts that would be displayed otherwise (all in category) or none.
I tried using query_posts() which also didn't work. I was also told:
manipulating taxonomies might require re-running meta queries processing.
I am using pre_get_posts() to allow all posts to be displayed on any category archive. This is because I will provide a javascript sorting and filtering method using isotope.js. Any category page will output all posts, but any that aren't a part of that category will initially be hidden.
function show_all_cats( $query ) {
if ( !$query->is_main_query() ){
return;
}
if ( is_admin() ){
return;
}
if ( $query->is_archive ) {
$query->set('cat', ''); //here is the problem
var_dump($query);
return;
}
return;
}
add_action( 'pre_get_posts', 'show_all_cats' );
I have tried setting 'cat' to 0, null, '' and '-' . $currentcategoryid.
They all either display only posts that would be displayed otherwise (all in category) or none.
I tried using query_posts() which also didn't work. I was also told:
Share Improve this question edited Jan 29, 2014 at 13:41 kaiser 51k27 gold badges151 silver badges245 bronze badges asked Jan 29, 2014 at 13:24 BillBill 16510 bronze badgesmanipulating taxonomies might require re-running meta queries processing.
4 Answers
Reset to default 1Just unsetting the cat variable probably isn't enough. The pre_get_posts hook happens after the query variables have already been parsed. So there's probably a tax_query with the taxonomy = category and the terms = your category.
You're already dumping the $query in your code, presumably for debugging. So, look at what you're actually dumping. Do you see the tax_query? Change your code to adjust that $query to have what you want it to have.
Have you tried this?
unset( $query->query_vars['cat'] );
You can use a filter pre_get_posts() to modify SQL query WHERE part:
add_filter( 'posts_where', function ( $where ) {
$where = preg_replace("regex pattern", "", $where);
return $where;
}, 10, 2 );
It is a bit hacking solution but should work.
It is about 4 years later, but if anyone needs the answer, this is the solution:
if ( $query->is_archive ) {
$q->set( 'category_name', '*your-category-slug*' );
}
Cheers
本文标签: taxonomyRemove category from query (show all posts in archivephp) pregetposts()
版权声明:本文标题:taxonomy - Remove category from query (show all posts in archive.php) pre_get_posts() 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749131996a2320745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论