admin管理员组文章数量:1277901
I've an archive page with custom post sorted by a custom field. I have two buttons that apply a filter by custom category. The result is not as desired. The correct number of results comes out, but the content of the posts does not match.
This is my pre_get_post in functions.php
function xgc_filter_archive( $query ) {
if ($query->is_main_query()){
if ( is_admin() ) {
return;
}
if ( is_post_type_archive ('concerts') ) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'categories_concerts',
'field' => 'slug',
'terms' => $_GET['cat'],
),
);
$query->set( 'tax_query', $taxquery );
}
}
if ( is_post_type_archive ('obres') ) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'categories_obres',
'field' => 'slug',
'terms' => $_GET['cat'],
),
)
;
$query->set( 'tax_query', $taxquery );
}
}
return $query;
}
}
add_action( 'pre_get_posts', 'xgc_filter_archive');
And this is a bit of my template:
<section id="menu_categories_obres" class="py-2">
<div class="filter-custom-taxonomy container justify-content-center">
<?php
$terms = get_terms( 'categories_obres' );
foreach ( $terms as $term ) : ?>
<a href="?getby=cat&cat=<?php echo esc_attr( $term->slug ); ?>" class="btn btn-md btn-primary mt-2" tabindex="-1" role="button" aria-disabled="true"><?php echo esc_html( $term->name ); ?>
</a>
<?php endforeach; ?>
</div>
</section>
<section id="llistat_obres" class="py-3">
<div class="container" style="display: flex;flex-wrap: wrap;">
<?php
// Variables
// query
$posts = get_posts(array(
'post_type' => 'obres',
'posts_per_page' => -1,
'meta_key' => 'any',
'orderby' => 'meta_value',
'order' => 'DESC'
));
if( $posts ):
while ( have_posts() ) : the_post();
If I comment main query works fine but then I am not sorted as I want.
Thanks for the replies
EDIT:
Now, If i put this code on functions.php
function xgc_filter_archive( $query ) {
if ($query->is_main_query()){
if ( is_admin() ) {
return;
}
if ( is_post_type_archive ('concerts') ) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'categories_concerts',
'field' => 'slug',
'terms' => $_GET['cat'],
),
);
$query->set( 'tax_query', $taxquery );
$query->set( 'post_type', 'concerts' );
$query->set( 'posts_per_page', -1 );
$query->set( 'meta_key', 'data_del_concert');
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'DESC' );
}
}
if ( is_post_type_archive ('obres') ) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'categories_obres',
'field' => 'slug',
'terms' => $_GET['cat'],
),
)
;
$query->set( 'tax_query', $taxquery );
$query->set( 'post_type', 'obres' );
$query->set( 'posts_per_page', -1 );
$query->set( 'meta_key', 'any');
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'DESC' );
}
}
return $query;
}
}
add_action( 'pre_get_posts', 'xgc_filter_archive');
IF I click the filter button goes well and the order too, but at the begining the order of all posts is wrong. This code isn't working well. Any suggest?
$posts_list = get_posts(array(
'post_type' => 'obres',
'posts_per_page' => -1,
'meta_key' => 'any',
'orderby' => 'meta_value',
'order' => 'DESC',
));
if( $posts_list ):
while ( have_posts() ) : the_post();
Thanks
I've an archive page with custom post sorted by a custom field. I have two buttons that apply a filter by custom category. The result is not as desired. The correct number of results comes out, but the content of the posts does not match.
This is my pre_get_post in functions.php
function xgc_filter_archive( $query ) {
if ($query->is_main_query()){
if ( is_admin() ) {
return;
}
if ( is_post_type_archive ('concerts') ) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'categories_concerts',
'field' => 'slug',
'terms' => $_GET['cat'],
),
);
$query->set( 'tax_query', $taxquery );
}
}
if ( is_post_type_archive ('obres') ) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'categories_obres',
'field' => 'slug',
'terms' => $_GET['cat'],
),
)
;
$query->set( 'tax_query', $taxquery );
}
}
return $query;
}
}
add_action( 'pre_get_posts', 'xgc_filter_archive');
And this is a bit of my template:
<section id="menu_categories_obres" class="py-2">
<div class="filter-custom-taxonomy container justify-content-center">
<?php
$terms = get_terms( 'categories_obres' );
foreach ( $terms as $term ) : ?>
<a href="?getby=cat&cat=<?php echo esc_attr( $term->slug ); ?>" class="btn btn-md btn-primary mt-2" tabindex="-1" role="button" aria-disabled="true"><?php echo esc_html( $term->name ); ?>
</a>
<?php endforeach; ?>
</div>
</section>
<section id="llistat_obres" class="py-3">
<div class="container" style="display: flex;flex-wrap: wrap;">
<?php
// Variables
// query
$posts = get_posts(array(
'post_type' => 'obres',
'posts_per_page' => -1,
'meta_key' => 'any',
'orderby' => 'meta_value',
'order' => 'DESC'
));
if( $posts ):
while ( have_posts() ) : the_post();
If I comment main query works fine but then I am not sorted as I want.
Thanks for the replies
EDIT:
Now, If i put this code on functions.php
function xgc_filter_archive( $query ) {
if ($query->is_main_query()){
if ( is_admin() ) {
return;
}
if ( is_post_type_archive ('concerts') ) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'categories_concerts',
'field' => 'slug',
'terms' => $_GET['cat'],
),
);
$query->set( 'tax_query', $taxquery );
$query->set( 'post_type', 'concerts' );
$query->set( 'posts_per_page', -1 );
$query->set( 'meta_key', 'data_del_concert');
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'DESC' );
}
}
if ( is_post_type_archive ('obres') ) {
if ( 'cat' === $_GET['getby'] ) {
$taxquery = array(
array(
'taxonomy' => 'categories_obres',
'field' => 'slug',
'terms' => $_GET['cat'],
),
)
;
$query->set( 'tax_query', $taxquery );
$query->set( 'post_type', 'obres' );
$query->set( 'posts_per_page', -1 );
$query->set( 'meta_key', 'any');
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'DESC' );
}
}
return $query;
}
}
add_action( 'pre_get_posts', 'xgc_filter_archive');
IF I click the filter button goes well and the order too, but at the begining the order of all posts is wrong. This code isn't working well. Any suggest?
$posts_list = get_posts(array(
'post_type' => 'obres',
'posts_per_page' => -1,
'meta_key' => 'any',
'orderby' => 'meta_value',
'order' => 'DESC',
));
if( $posts_list ):
while ( have_posts() ) : the_post();
Thanks
Share Improve this question edited Nov 21, 2021 at 18:13 Joan Chaparro asked Nov 16, 2021 at 8:10 Joan ChaparroJoan Chaparro 13 bronze badges1 Answer
Reset to default 0Use the different variable names on the place of $posts
. You should not use any of the global variable names for avoiding such incidents.
Also, call wp_reset_query()
after fetching your data. This will reset the query to default.
For the list of global variables please refer to this link.
Edit your query to read like this:
$posts_list = get_posts(array(
'post_type' => 'obres',
'posts_per_page' => -1,
'meta_key' => 'any',
'orderby' => 'meta_value_num',
'order' => 'DESC',));
if( $posts_list ):
while ( have_posts() ) : the_post();
本文标签: custom taxonomyMain query disturb pregetpost by category query
版权声明:本文标题:custom taxonomy - Main query disturb pre_get_post by category query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741210301a2358978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论