WordPress.org

Forums

WP_Query and custom taxonomies (9 posts)

  1. d910qf
    Member
    Posted 5 years ago #

    $args = array('post_type' => 'message', $taxonomy => $term, 'posts_per_page' => 25, 'paged' => $paged );
    $wp_query = new WP_Query($args);

    The code above gets ALL post_types (not just messages) for the taxonomy term $term.

    But if we change it to:

    $args = array('post_type' => 'message', 'posts_per_page' => 25, 'paged' => $paged );
    $wp_query = new WP_Query($args);

    it gets the correct post_types of 'message'. I can't seem to get any query to work correctly that combines post_type with a taxonomy selection - but they work fine individually.

    I have read everything I can and am sure the top section of code is correct - does anyone see anything wrong with it?

  2. d910qf
    Member
    Posted 5 years ago #

    Sent email to wp-hackers list:

    The post_type is ignored for taxonomy based queries. See trunk,
    query.php, line 2034.

    -Otto

  3. kalyx
    Member
    Posted 5 years ago #

    I'm looking for the same result. Is there someone who find a solution?

  4. MichaelH
    Member
    Posted 5 years ago #

    That problem was fixed in the 3.0 beta.

  5. Anointed
    Member
    Posted 5 years ago #

    having same exact problem running latest trunk.

    <?php
    $wpq = array (post_type =>'movies','taxonomy'=>'series','term'=>$term->slug);
    $query = new WP_Query ($wpq);
    while ( have_posts()) : the_post(); ?>
    
    <?php the_title(); ?>
    do other stuff
    
    <?php endwhile; ?>

    returns all post_types not just movies.

    Is my code syntax wrong or something as you state it works in beta?

  6. Mark Rowatt Anderson
    Member
    Posted 5 years ago #

    I was having similar problems (3.0b2), but found this worked:-

    $wpq = array (post_type =>'movies','series'=>$term->slug);

    For this to work, though, when the taxonomy is setup the 'query_var' parameter should either not be set or (in this case) set to 'series'.

  7. Anointed
    Member
    Posted 5 years ago #

    @markauk
    Here is what I have figured out.

    1. If I only have a taxonomy applied to a single post_type, then the above function works perfectly.

    2. If I have a taxonomy 'series' applied to multiple post_types then it does not do the multi filter. It returns the output of both post_types.

    Meaning, I have the 'series' taxonomy applied to both 'movies, albums'. When I run the above function it always outputs the values for both movies and values with the taxonomy 'series'.

    definitely a bug.

    thanks

  8. Mark Rowatt Anderson
    Member
    Posted 5 years ago #

    @anointed
    It works for me with taxonomies attached to multiple post types.

    Here's the code I am using (it's extracted from various places in the site I am working on, but I think that's all the essential bits):-

    (1) register the new post type

    register_post_type( 'history',
    	array(
    			'label'	=>	'History',
    			'public' => true,
    			'show_ui' => true,
    			'publicly_queryable' => false,
    			'exclude_from_search' => true,
    			'hierarchical' => false,
    	)
    	);

    (2) register the taxonomy

    register_taxonomy('featured', array('page','post','history'), array(
    		'hierarchical' 	=> TRUE,
    		'label' 			=> 'Featured Pages',
    		'singular_label'	=> 'Featured Page',
    		'public' 			=> TRUE,
    		'show_ui' 			=> TRUE,
    		'show_tagcloud' 	=> FALSE,
    		'rewrite' 			=> FALSE
    	));

    (3) do the query

    $args = array(
    		'post_type'	 =>	array('page','post','history'),
    		'post_status' =>	'publish',
    		'featured'	=> 'test',
    	);
    
    $wp_query = new WP_Query($args);
  9. Anointed
    Member
    Posted 5 years ago #

    @markauk
    It turns out that I identified not a 'bug' per say, but functionality that currently does not exist in wp.
    http://core.trac.wordpress.org/ticket/13582

    Basically we can't currently filter via post type + taxonomy dynamically yet.

    Michael said:

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

    This is all pushing the boundaries of my abilities, but I am now trying to figure out how to add the functionality to the core. I've never before written direct db queries logic before. Heck, not even sure what the proper terminology is.

    I'm sure that many people are going to want to do this, so I am trying as hard as I can to get it figured out for not just me but for everyone.

Topic Closed

This topic has been closed to new replies.

About this Topic