How to filter posts by format and category via url?

I figured out I can filter posts by format just by doing /type/{format} e.g. /type/gallery/ in the url.

Looking for a way to filter by categories on top of this, something like /type/gallery/category/installation or /type/gallery/art.

Read More

I’ve figured out the query, but I don’t know how to handle the url bit:

$galleryquery = wp_parse_args($query_string);
$galleryquery['tax_query'] = array(
    array(
        'taxonomy' => 'post_format',
        'terms' => array('post-format-gallery'),
        'field' => 'slug',
    ),
    array(
        'taxonomy' => 'category',
        'terms' => array('installation'),
        'field' => 'slug',
    ),
);
query_posts($galleryquery);

Ideas? I’m guessing I have to intercept the permalink and somehow pass it to my query…

Site is reachable here

Related posts

Leave a Reply

3 comments

  1. I am doing something similar, but for member pages. I was able to use the code in my answer linked here which parses the information in the URL which can be manipulated within WordPress.

    https://wordpress.stackexchange.com/a/91399/12920

    Here’s a snippet of the URL handling portion which perhaps will get you started? Granted you’re doing something different than me, but the underlying function should be very similar.

    // Catch the URL and redirect it to a template file
    function userpage_rewrite_catch() {
        global $wp_query;
    
        if ( array_key_exists( 'member', $wp_query->query_vars ) ) {
            // Do something if member is in the URL
            include (TEMPLATEPATH . '/user-profile.php');
            exit;
        }
    }
    add_action( 'template_redirect', 'userpage_rewrite_catch' );
    
  2. A possible approach is to create a new rewrite rule to handle the custom url. I haven’t tested the code, but it goes something like this:

    function add_category_query_var($query_vars) {
        array_push($query_vars, 'post_type');
        array_push($query_vars, 'post_category');
        return $query_vars;
    }
    
    add_filter('query_vars','add_category_query_var');
    
    /* create new rewrite rule*/
    function add_category_rewrite_rule($wp_rewrite_rules) {
    
            $new_rewrite_rule['type/([^/]+)/category/([^/]+)/?$'] = 'index.php?post_type=$matches[1]&post_category=$matches[2]';
    
            $wp_rewrite_rules = $new_rewrite_rule + $wp_rewrite_rules;
    
            return $wp_rewrite_rules;
    }
    
    add_filter('rewrite_rules_array', 'add_category_rewrite_rule');
    

    Then you would access the new variable like so:

    $galleryquery['tax_query'] = array(
        array(
            'taxonomy' => 'post_format',
            'terms' => array('post-format-gallery'),
            'field' => 'slug',
        ),
        array(
            'taxonomy' => 'category',
            'terms' => array($_GET['post_category']),
            'field' => 'slug',
        ),
    );
    
  3. I ended up with this:

    function when_rewrite_rules( $wp_rewrite ) {
      $newrules = array();
      $new_rules['type/([^/]+)/category/([^/]+)/?$'] = 'index.php?post_format=$matches[1]&category_name=$matches[2]';
      $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    add_filter('generate_rewrite_rules','when_rewrite_rules');
    

    There is no need to add new query vars. You can get both taxonomies out of the box: categories are query-able with “?category_name=x” and post formats with “?post_format=x”.

    Confusingly, post formats are a taxonomy, and not a custom post type, yet have the taxonomy of “type”. E.g. you filter them like so:

    mysite.com/type/gallery

    Seems like “/format/” would be a better taxonomy to avoid confusion with Custom Post Type.