WordPress.org

Ready to get started?Download WordPress

Forums

Smart Archives Reloaded
[resolved] Custom Taxonomies (9 posts)

  1. Doodlebee
    Member
    Posted 2 years ago #

    Just a question: I have this working nicely with custom post types. However, I want to show results ONLY for a single specific category (a custom taxonomy for the custom post type) - but cannot make it work.

    Usually I use Otto's advanced custom taxonomy query tutorial, but apparently the query within the plugin doesn't allow me to access the tax_query to make that specification in the manner presented by Otto.

    So I'm kind of stumped. Is there a way I can add some sort of filter to the regular query this plugin uses so I can pull this off?

    Thanks so much :)

    http://wordpress.org/extend/plugins/smart-archives-reloaded/

  2. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    How are you passing the custom post type to the query?

  3. Doodlebee
    Member
    Posted 2 years ago #

    That's the question, what I'm trying isn't passing it. So far, I've got this, but I'm pretty sure I'm going about this the wrong way:

    class SAR_Custom_Generator extends SAR_Generator {
    
     protected function generate_fancy() {
    	function custom_tax_filter($query) {
       		 $query->set('tax_query', array(
                'relation' => 'AND',
                array(
                      'taxonomy' => 'author_category',
                      'field' => 'slug',
                      'terms' => 'campaigns'
                      )
                )
               );
              return $query;
    	}
    	add_filter( 'pre_get_posts', 'custom_tax_filter' );
     }
    
    }

    'cause it's totally not doing anything at all. (I think it's probably in the add_filter section, mainly because I think this needs to happen before when I'm calling it, if that makes sense). I'm still playing with it to see what's happening.

  4. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    You can pass query params directly to the smart_archives() template tag:

    <?php smart_archives( '', array(
      'tax_query' => array(
        ...
      )
    ) ); ?>
  5. Doodlebee
    Member
    Posted 2 years ago #

    Yes! That's what I originally tried (it made the most sense) but it wasn't working. here's what I had before I tried the magled stuff above:

    $query = array('orderby' => 'author',
    			  'post_type' => 'author_posts',
    			  'tax_query' => array('taxonomy' => 'author_category',
    					    		  'terms' => 'campaigns',
    					    		  'field' => 'slug'
    					    		  )
    			);
    
    smart_archives('format=fancy', $query);

    it seems it doesn't mix well. if I leave off the "orderby" and "post _type", and just stick with the tax_query, it returns regular posts (but why, I have no idea, since there are no regular posts using the custom taxonomy); but if I leave off the tax_query, it'll return all the author_posts (custom post type) - which is what it should do - but I also need it to only return the custom post type that's in that custom taxonomy.

  6. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    Does calling WP_Query directly with those parameters return the correct posts?

  7. Doodlebee
    Member
    Posted 2 years ago #

    ah, excellent test - thank you. And no, it's not returning the correct stuff - so something's gotta be wrong with my syntax...

  8. Doodlebee
    Member
    Posted 2 years ago #

    Oh wow. it was WAY easier than I thought! Here's what works:

    smart_archives('format=fancy', array('orderby' => 'author',
    				'post_type' => 'author_posts',
    				'author_category' => 'campaigns'
    			)
    		);

    Hope that helps someone else :)

  9. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    Yeah, the problem was that you were missing a level of array nesting inside 'tax_query', but 'tax_query' doesn't make much sense for a single taxonomy anyway.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic