1

I have a custom taxonomy template page in my theme. Essentially, when someone clicks on a term link into this taxonomy, I -ALWAYS- want to add a specific post-type to the query.

I'm not entirely sure how to do this, I've found some functions that are included in PHP 5.3 that seem like they might do it, but I have 5.2 installed on my host.

But, I essentially want to take the current $wp_query, add 'post-type' => 'my-custom-post-type' and then continue on with $wp_query in my loop. Is this possible?

I'm probably over tired and over stressed and there's a much simpler way to do this and I just can't think of it =D

Thanks!

EDIT----

Ok, Clearly my methodology here is WAY off. Probably a direct result of being tired.

I did manage to find code to manipulate $wp_query to add what I wanted; but it did not give the desired result:

function _my_method(&$item, $key) {
    if ($key == 'post_type') {
        $item = 'backend_data';
    }
}
array_walk_recursive( $wp_query , '_my_method' );

So, in the QUERY_VARS part of the array, now post_type has a value, backend_data. Except that farther on in the $wp_query array I can still see it querying every post type available.

[request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN 
wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( 
wp_term_relationships.term_taxonomy_id IN (35) ) AND wp_posts.post_type IN ('post', 
'page', 'attachment', 'company', 'backend_data', 'ps_promotion') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') GROUP BY 
wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10 [posts] => Array ( [0] => 
backend_data [1] => stdClass Object ( [ID] =>

and then the two posts its pulling which are in 'posts'... it does however list backend_data again before it starts pulling post information. But I clearly don't know enough about the inner workings of wordpress and arrays to understand WTF it's doing at this point =D

EDIT -----

Okay, now I'm just convinced my brain has gone mushy and there has got to be a more wordpressy way to do this cause php array hacking doesn't do the trick =D I tried throwing some wp_reset_query(); first before and then after my above function, and it still outputs the same thing with all post-types accounted for.

So, I guess I added all this to my question for no reason :(

1 Answer 1

4

You can use pre_get_posts filter hook to check if you are on the taxonomy page and if so just add your custom post type to the query something like this:

function add_my_type( $query ) {
    if ( $query->is_tax('YOUR_TAXONOMY') ) {
        $query->set( 'post_type', array('post','YOUR_CUSTOM_POST') );
    }
}
add_filter( 'pre_get_posts', 'add_my_type' );
1
  • Ah-ha! Thank you so much Bainternet! You missed a close-parenthesis on line 3 but otherwise works great!
    – Sam K
    Commented Jun 7, 2011 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.