• Resolved spartanv7

    (@praeix)


    Hello all,

    I’m really stuck on a problem and I’m not understanding it at all. I’ve read up on how to use taxonomy queries and it sounds like based on my data structure, this should work:

    I want to show all equipment items that have a category slug = ‘type1’ and a location slug = ‘location1’. Here is how my query is structured:

    $location = 'location1';
    $type = 'type1';
    
    // Build the args
    $args		= array(
    	'post_type'	=> 'equipment',
    	'orderby'	=> 'ID',
    	'order'		=> 'ASC',
    	'posts_per_page' => -1,
    	'tax_query'	=> array(
    		'relation' => 'AND',
    		array(
    			'taxonomy'	=> 'category',
    			'field'		=> 'slug',
    			'terms'		=> array( $type )
    		),
    		array(
    			'taxonomy'	=> 'locations',
    			'field'		=> 'slug',
    			'terms'		=> array( $location )
    		)
    	)
    );
    
    // Query the posts
    $posts = query_posts( $args );

    The issue here is that the query returns ALL of the equipment items that belong to the location given, but it ignores the category “type” specification. I can’t figure out why because based on all of the examples I’ve read, this should work just fine.

    Any ideas? Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter spartanv7

    (@praeix)

    I switched the line:
    $posts = query_posts( $args );

    to:

    $posts = new WP_Query( $args );

    to see what the query was that is happening. Here it is below:

    SELECT esp_posts.* FROM esp_posts INNER JOIN esp_postmeta AS cpt_onomy_pm1 ON (esp_posts.ID = cpt_onomy_pm1.post_id AND cpt_onomy_pm1.meta_key = '_custom_post_type_onomies_relationship') WHERE 1=1 AND esp_posts.post_type = 'equipment' AND (esp_posts.post_status = 'publish' OR esp_posts.post_status = 'private') AND ( cpt_onomy_pm1.meta_value IN (51) ) GROUP BY esp_posts.ID ORDER BY esp_posts.ID ASC

    I’m using CPT_onomies to create the “locations” taxonomy from a custom post type called “locations” so that it is dynamic.

    Could this be causing the issue? I’m an idiot when it comes to breaking down SQL queries so I’m not sure exactly why this query wouldn’t be producing the results that I’m after.

    Thread Starter spartanv7

    (@praeix)

    RESOLVED!

    The link is here for anybody else wondering:

    CPT-onomies: Using Custom Post Types as Taxonomies

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Query Not Working with Taxonomies’ is closed to new replies.