0

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

1 Answer 1

0

Use 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();
22
  • Thanks BlueSuiter. where I need to change the name variables and where should I add wp_reset_query ()? thanks Commented Nov 16, 2021 at 11:43
  • BlueSuiter I changed & posts to $ posts_list and it worked !!! But now the sorting by the field 'ANY' does not work. Commented Nov 16, 2021 at 11:51
  • @JoanChaparro Is it a numeric value?
    – BlueSuiter
    Commented Nov 16, 2021 at 12:15
  • yes is a year. I edited the initial post,... some suggest? Thanks Commented Nov 16, 2021 at 12:17
  • Change 'orderby' => 'meta_value', to 'orderby' => 'meta_value_num',
    – BlueSuiter
    Commented Nov 16, 2021 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.