Make WordPress Core

Opened 13 years ago

Closed 13 years ago

#13582 closed enhancement (fixed)

Allow filtering via post_type + taxonomy

Reported by: anointed's profile anointed Owned by: scribu's profile scribu
Milestone: 3.1 Priority: normal
Severity: normal Version: 3.0
Component: Posts, Post Types Keywords: post_types, taxonomies
Focuses: Cc:

Description

I have found that 3.0nightly does not allow me to sort via both post_type and taxonomy term, when the tax term is applied to multiple post_types.

I have a custom taxonomy 'series' registered to 2 separate post_types, 'movies and television'.

Working on creating a custom taxonomy page that will return the results for only 'movies' with a tax term of 'series' in my file taxonomy-series.php

Example script:

$my_query = get_posts( array(
	'numberposts' => 5,
	'post_type' => 'movies',
	'series' => $term->taxonomy
//have also tried in place of series above
'taxonomy' => $term->taxonomy
	) );
if( $my_query ) {
	print '<h2>Resources</h2>';
	foreach( $my_query as $post ) {
		setup_postdata( $post );
		the_title( '', ' - <b>' . $post->post_type . '</b><br />' );
		the_excerpt();
	}
}

Another example

<?php
$wpq = array (post_type =>'movies','taxonomy'=>'series';
$query = new WP_Query ($wpq);
while ( have_posts()) : the_post(); ?>

<?php the_title(); ?>
do other stuff

<?php endwhile; ?>

Basically it seems that no matter what function combination I try, I always get the results returned containing both movies and television for the tax series.

The functions do work, when there is only a single post_type with the taxonomy term. The problems only arise when there is more than one post_type involved.

Change History (13)

#1 @michaelh
13 years ago

If you have registered a taxonomy called series, and for some of those movies let's say you used "xyz" for the series.

You should be able to get all published movies assigned the series "xyz" with this:

<?php
$args=array(
  'series'=>'xyz',
  'post_type' => 'movies',
  'post_status' => 'publish',
  'posts_per_page' => -1
);
$my_query = null; 
$my_query = new WP_Query($args); 
if( $my_query->have_posts() ) { 
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
} 
wp_reset_query(); 
?>

If you are trying to get all movies that are assigned any series value, you would need to iterate through your series terms, and get the movies in each term...like:

<?php 
$post_type = 'movies';
$tax = 'series';
$tax_terms = get_terms($tax);
if ($tax_terms) {
  foreach ($tax_terms  as $tax_term) {
    $args=array(
      'post_type' => $post_type,
      "$tax" => $tax_term->slug,
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
  
    $my_query = null; 
    $my_query = new WP_Query($args); 
    if( $my_query->have_posts() ) { 
      echo 'List of '.$post_type . ' where the taxonomy '. $tax . '  is '. $tax_term->name;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    } 
    wp_reset_query();   
  }
}
?>

#2 @anointed
13 years ago

I am trying a variation of the top function you provided.

When using 'series'=>'xyz' it works

However, 'xyz' is not known until a person clicks a link to get to the page. xyz should be determined via the slug.

Another example:

<?php
$args=array(
  'post_type' => 'podcasts',
  'taxonomy' => 'series',
  'term' => $term->slug,
  'post_status' => 'publish',
  'posts_per_page' => -1
);
$my_query = null; 
$my_query = new WP_Query($args); 
if( $my_query->have_posts() ) { 
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
//do stuff
<?php
  endwhile;
} 
wp_reset_query(); 
?>

maybe an example page would help:
http://deardaddy.org/series/the-anointing/

using the function above, the concept is easy.
I have 2 sets of displays 'sermons, podcasts' both of which use the taxonomy 'series'

This page should only return the sermons belonging to taxonomy series 'the-anointing', followed by podcasts belonging to taxonomy series 'the-anointing'.

The Problem:
Sermons is returning all taxonomy series, not just 'the-anointing' as is podcasts.

'the-anointing' is the slug value.

Below is how I registered 'series'

register_taxonomy('series', array('post','page','sermons','podcasts'),
array(
	'labels' =>array(
	    'name' => _x('series', 'taxonomy general name'),
	    'singular_name' => _x( 'series', 'taxonomy singular name'),
	    'search_items' => __( 'Search Series' ),
	    'popular_items' => __( 'Popular Series' ),
	    'all_items' => __( 'All Series' ),
	    'parent_item' => __( 'Parent Series' ),
	    'parent_item_colon' => __( 'Parent Series:' ),
	    'edit_item' => __( 'Edit Series' ),
	    'update_item' => __( 'View Series' ),
	    'add_new_item' => __( 'Add New Series' ),
	    'new_item_name' => __( 'New Series Name' ),
    	    ),
   	 'hierarchical' => true,
   	 'show_ui' => true, 
   	 'query_var' => true,
   	 'rewrite' => array( 'slug' => 'series' ),
   	 )
   	 );

I'm guessing there is a bug somewhere along the line, but man I can't find it.

#3 @michaelh
13 years ago

Try this to get podcasts assigned the series, the-anointing:

$args=array(
  'post_type' => 'podcasts',
  'series' => 'the-anointing',
  'post_status' => 'publish',
  'posts_per_page' => -1
);

This is probably something for the forums rather than trac, but will leave this for you to close.

#4 @anointed
13 years ago

Michael

Yes that works, but this is dynamic not hardcoded.

The idea being a user goes to my primary 'sermon' page
http://deardaddy.org/sermons/

They see a sermon there that says it's in the custom_taxonomy 'series' term = 'the-anointing' so they click that. It sends them to the page
http://deardaddy.org/series/the-anointing/

The above link represents my taxonomy-series.php file for the series 'the-anointing'.

I'm basically trying to do a dual sort + termslug

post_type = 'sermons' + custom taxonomy = 'series' plus term = term->slug

I actually did post it in the forums, but after talking to a lot of people consensus was either my code was wrong or it's a bug. was suggested to me to post here. If I am wrong, I really appologize. I don't take posting bugs lightly, and spent days trying before coming here.

#5 @anointed
13 years ago

To add:
Doing it your way would require me to build a separate template file for each and every 'series' term. As those are added by the users, I don't think that is even feasible. There will be hundreds of series.

#6 @anointed
13 years ago

hmm can't edit posts here:

$args=array(
  'post_type' => 'podcasts',
  'taxonomy' => 'series',
  'term' => $term->slug,
  'post_status' => 'publish',
  'posts_per_page' => -1
);

I think that is the most relevent part of the issue. The 3rd sort which does not work,
'term' => $term->slug,

#7 @michaelh
13 years ago

Both taxonomy or term are invalid query variables--see: http://codex.wordpress.org/Template_Tags/query_posts

In your case, the query var available is:
'series' => 'some value'

A url such as post_type = 'sermons' + custom taxonomy = 'series' plus term = term->slug is not supported.

Changing this to an enhancement request.

#8 @michaelh
13 years ago

  • Summary changed from filtering via post_type + taxonomy does not work properly. to Allow filtering via post_type + taxonomy
  • Type changed from defect (bug) to enhancement

#9 @anointed
13 years ago

Thanks Michael

I just figured it was a bug as it's seemed like completely logical functions for a site with custom post_types. I would have never guessed that the functionality wasn't inherant in wp.

I'm hoping that as 3.0 with post_types becomes the norm, that lots of people will see the need to sort and it makes it in.

In the meantime I suppose that I will simply have to remove the links to all my post_type terms on my themes so that people don't try searching them.

#10 @kevinB
13 years ago

  • Cc kevinB added

#11 @scribu
13 years ago

  • Owner set to scribu
  • Status changed from new to reviewing

#12 @scribu
13 years ago

  • Milestone changed from Awaiting Review to 3.1

#13 @scribu
13 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

(In [15850]) Re-add the ability to filter by 'taxonomy' and 'term' query vars. Fixes #13582. See #12891

Note: See TracTickets for help on using tickets.