Custom Post Type Taxonomy Filters

Im really struggling to piece this functionality together, and hope someone can help or provide snippets to get me there.

So I’m creating a section on a website where stories are published, I have created a custom post type for the stories section and taxonomies for the story types (category) and story tags (the tags). Everything is working fine and displaying correctly in that respect.

Read More

Heres the setup details:

  • Custom Post Type: stories
  • Custom Taxonomy (the category): story-type
  • Custom Taxonomy (the tags): story-tags

I need to be able to filter posts on a landing page by 4x options:

  • Story Type Taxonomy (Chapter or Extract, Featured, Flash Fiction,
    Micro Fiction, Poem, Short Story)
  • Story Tags Taxonomy (Debut, Funny, True Story)
  • by Month (jan, feb, march, etc…)
  • Sort by (most read, most recent)

So an example: Please show me all Fiction stories tagged funny in June and sort them by most read.

I’m not fussy where the results are displayed as long as they are accurate.

The results page would then be titled “Your Results”

Thanks for your time so far…

Related posts

2 comments

  1. I would take a look at this page on the Codex:

    http://codex.wordpress.org/Class_Reference/WP_Query

    I think you need to override your query_posts() for your landing page, and controlling different queries with if() statements, depending on what areas of a form (guessing you’re using a form?) has filled in. For example:

    // Carry pagination through
    $args = array(
      'paged' => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1
    );
    
    // Check if 'story-type' was searched for
    if ( !empty( $_POST['story_type'] ) )
      $args['story-type'] = $_POST['story_type'];
    
    // Check if 'story-tags' was searched for
    if ( !empty( $_POST['story_tags'] ) )
      $args['story-tags'] = $_POST['story_tags'];
    
    // Check if a year was selected
    if ( !empty( $_POST['date_year'] ) )
      $args['year'] = $_POST['date_year'];
    
    // Check if a month was selected
    if ( !empty( $_POST['date_month'] ) )
      $args['month'] = $_POST['date_month'];
    
    // Override $wp_query
    query_posts( $args );
    

    The sorting is done in the same way – check out ‘order’ and ‘orderby’.

  2. You’ll probably want to ignore this answer. I read the question completely wrong.

    You’ll be needing the tax_query inside a WP_Query. There’s examples under the Taxonomy Parameters section on the WP_Query docs on Codex.

    An example (taken from Codex) of searching multiple taxonomies:

    $args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'movie_genre',
            'field' => 'slug',
            'terms' => array( 'action', 'comedy' )
        ),
        array(
            'taxonomy' => 'actor',
            'field' => 'id',
            'terms' => array( 103, 115, 206 ),
            'operator' => 'NOT IN'
        )
    )
    ); 
    $query = new WP_Query( $args );
    

Comments are closed.