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 ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) ) {
$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