Site icon Hip-Hop Website Design and Development

Main query disturb pre_get_post by category query

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