1

The need: I'm trying to filter a custom post type archive using a GET parameter, so that I can split the posts in two groups depending on which terms they are tagged with.

How: So far I've decided to use the pre_get_posts hook, altering the query when the archive for this custom post type is rendered.

The problem: The code I'm using is not working. I can't seem to find the issue, but it is definitely not working. Maybe someone who has more exp with this hook may tell me what I'm missing:

function event_type_filter( $query ) {
    if ( is_post_type_archive( 'event' ) ) {

        if ( isset($_GET["event_term"]) ) {
            $tipo_de_curso = $_GET["event_term"];
            if ( $tipo_de_curso == "singleclass" ) {
                $query->set( 'cat', '1,2,3,4' );
            }
            if ( $tipo_de_curso == "course" ) {
                $query->set( 'cat', '-1,-2,-3,-4' );
            }
        }
    }
return $query;
}
add_action('pre_get_posts','event_type_filter');

Here in the Codex they recommend a similar approach, and I can't spot the differences, but sure there has to be some. There is some questions in this site about this hook that should serve as a model, but I didn't see how to adapt to my case.

Any ideas? Thanks :)

5
  • 1
    tried removing return $query; ? Commented Feb 18, 2015 at 9:08
  • also on your if statement if ( is_post_type_archive('event') && ! empty( $query->query['post_type'] == 'event' ) Commented Feb 18, 2015 at 9:12
  • removing the return $query; does not have an effect, but neither had the addition, to begin with. (I already tried with and without it.). Also, I completed the if statement, but I cant notice any changes. Thanks @Reigel
    – versvs
    Commented Feb 18, 2015 at 10:11
  • 1
    Depending on the URI is_post_type_archive() may fail - See the Notes section in the Codex. Perhaps replace if(is_post_type_archive('event')) with if(get_query_var('event'))?
    – David Gard
    Commented Feb 18, 2015 at 10:46
  • 2
    FYI, when you use and action hook (add_action()), the function that you reference doesn't need to return anything. On the other hand, if you are using a filter hook (add_filter()) then you do need to return a value. So in your case the line return $query; is superfluous - it's not causing your code to fail, but you don't need it.
    – David Gard
    Commented Feb 18, 2015 at 10:50

1 Answer 1

4

You should be using methods of the $query instance instead of the is_* template tags. And are the categories you are trying to filter a custom taxonomy? cat only works on the default category taxonomy:

function wpse_178500_event_type_filter( $wp_query ) {
    if ( ! empty( $_GET['event_term'] ) && $wp_query->is_main_query() && $wp_query->get( 'post_type' ) === 'event' ) {
        $tipo_de_curso = $_GET['event_term'];

        if ( ! $tax_query = $wp_query->get( 'tax_query' ) )
            $tax_query = array();

        if ( $tipo_de_curso === 'singleclass' ) {
            $tax_query[] = array(
                'taxonomy' => 'category', // Make sure this is correct! "category" is the default post category.
                'field' => 'term_id',
                'terms' => array( 1, 2, 3, 4 ),
            );

        } elseif ( $tipo_de_curso === 'course' ) {
            $tax_query[] = array(
                'taxonomy' => 'category', // Make sure this is correct! "category" is the default post category.
                'operator' => 'NOT IN',
                'field' => 'term_id',
                'terms' => array( 1, 2, 3, 4 ),
            );
        }

        $wp_query->set( 'tax_query', $tax_query );
    }
}

add_action( 'pre_get_posts','wpse_178500_event_type_filter' );
2
  • You nailed it. I was trying to filter terms in a custom taxonomy. Your code works like a charm. I wonder: Is this the only way to achieve this on custom taxonomies?
    – versvs
    Commented Feb 18, 2015 at 11:17
  • 1
    You can use taxonomy => term_slug, but that's about it. tax_query is almost always the way to go. Commented Feb 18, 2015 at 11:20

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.