• g3legacy

    (@g3legacy)


    Hi, so I’ve read up on this a lot, and I’m still having issues getting the data I need. Here’s the task…

    I have 6 custom post types, they all share one taxonomy which has many terms. On my home page I need to get the top 4 most used/popular terms for each custom post type. I’ve discovered I might be able to do with with wp_query, a little like my code (which doesn’t work, it’s ignoring the taxonomy and calling all posts from the CPT);

    $args = array(
    	'post_type' => 'pt_plumbers',
    	'tax_query'=>array(
    		'taxonomy'=>'trade',
    		'field'=>'slug',
    		'term'=>'boiler-installation'
    	)
    );

    To fully make this work I think I will be using a second “hidden” taxonomy which is unique to each CPT, making the query feasible for each CPT (so I don’t just get the actual top 4 terms across the entire site, it must be post type specific).

    Hope that makes sense, hit me with what you’ve got while I still have some hair 😀

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Ross Hanney

    (@rosshanney)

    Hello,

    tax_query requires an array of arrays, if that makes sense!

    So, your code should probably be as follows:

    $args = array(
    	'post_type' => 'pt_plumbers',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'trade',
    			'field' => 'slug',
    			'term' => 'boiler-installation'
    		)
    	)
    );

    This is described in more detail here (specifically, the “Important Note” section).

    Thread Starter g3legacy

    (@g3legacy)

    I’ve read the codex page again, and it looks like you’re right! Sadly it still finds no posts. Just to make sure I understand this…

    – My custom taxonomy is called “trade”
    – One of my terms/categories under trade is “boiler-installation”
    – I’m querying using the slug.

    That sounds simple enough so what am I doing wrong?

    Ross Hanney

    (@rosshanney)

    That sounds like it should be ok.

    Where abouts is this code? In a template file / in functions.php / in a plugin etc?

    Could you post all the code you’re using (perhaps at pastebin)? It might be easier to figure out the problem in context.

    Thread Starter g3legacy

    (@g3legacy)

    The post type is made in a plugin file, but that’s all working as it should be. The query is in a template file;

    $args = array(
    	'post_type' => 'pt_plumbers',
    	'tax_query'=>array(
    			array(
    			'taxonomy'=>'trade',
    			'field'=>'slug',
    			'term'=> 'boiler-installation'
    			)
    	)
    );
    $plumber_tax = new WP_Query($args);
    print_r($plumber_tax);
    if($plumber_tax->have_posts()){
    	while($plumber_tax->have_posts()){
    		$plumber_tax->the_post();
    			the_title();
    	}
    }
    Ross Hanney

    (@rosshanney)

    Ah, looks like it might just be a small typo. In tax_query, term should be terms, like this:

    $args = array(
    	'post_type' => 'pt_plumbers',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'trade',
    			'field'    => 'slug',
    			'terms'    => 'boiler-installation'
    		)
    	)
    );

    I didn’t notice at first!

    Thread Starter g3legacy

    (@g3legacy)

    Thanks for the pointer – silly me!

    I need to figure out how to add my “hidden” taxonomy in the query now. Oh, the woes of PHP. Good fun when it goes well, despair when it doesn’t 😀

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WP_Query with custom taxonomies and post types…!’ is closed to new replies.