Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is a cross post from the WordPress forums, which I apologize for, but it has been almost a week and not getting any response there so hoping someone here can help. If I do get a reply in either place I will update the post with a link to it.

I am attempting to extend functionality to my client's custom theme, and I am having issues trying to get query_posts() to pull a list of custom taxonomies filtered by category. The creation of the custom post type in functions.php is this:

//Custom Post Types
add_action('init', 'create_myportfoliotype');
function create_myportfoliotype() {
    $myportfoliotype_args = array(
        'label' => __('Portfolio'),
        'singular_label' => __('Portfolio'),
        'public' => true,
        'show_ui' => true,
        'menu_position' => 5,
        'capability_type' => 'post',
        'hierarchical' => false,
        'publicly_queryable' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ),
        'can_export' => true,
        'supports' => array(
            'title', 
            'editor', 
            'post-thumbnails',
            'custom-fields',
            'page-attributes',
            'author',
            'thumbnail'
          )
       );
  register_post_type('myportfoliotype',$myportfoliotype_args);
}


//Taxonomias
register_taxonomy("categories", array("myportfoliotype"), array("hierarchical" => true, "label" => "Category", "singular_label" => "Category", "rewrite" => true));
register_taxonomy( 'tags', array("homepage"), array( 'hierarchical' => false, 'label' => 'Tags', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'specifics', array("myportfoliotype"), array( 'hierarchical' => false, 'label' => 'Specifics', 'query_var' => true, 'rewrite' => true ) );

Currently the site has a page that pulls in all of the posts, then filters out what is visible via jquery. The query that pulls in the posts is this:

query_posts( array( 'post_type' => 'myportfoliotype', 'paged' => $paged, 'posts_per_page' => 80))

This works, but as I said pulls in everything. What they want now is 2 custom templates that pulls in only 2 specific categories each. I can't do the filtering via jquery since it doesn't work visually to do that in this case. I am attempting to pull in just the 2 categories, but for some reason no variation of the following code seems to work:

query_posts( array( 'post_type' => 'myportfoliotype', 'paged' => $paged, 'posts_per_page' => 80, 'tag_id' => '9' ))

Everything I have tried either still pulls in all posts, or pulls in none of them.

ps. Does anyone know of a function to dump the raw query that query_posts generates? If I could see what the actual query was doing with each variation I tried and compared that to the info in wp_postmeta it might help in figuring out where the issue lies.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

This is not a tought quest, but you'd have to read carefully the Codex. Especially tax_query part of WP_Query.

Your proposed query_posts call is wrong. This should look like this:

query_posts( array(  
    'post_type' => 'myportfoliotype', 
    'paged' => $paged, 
    'posts_per_page' => 80, 
    'tax_query' => array( 
        array( 
            'taxonomy' => 'category', //or tag or custom taxonomy
            'field' => 'id', 
            'terms' => array('9') 
        ) 
    ) 
) );

Further, to gain more efficiency of using DB queries, you should consider using pre_get_posts hook instead of query_posts

share|improve this answer
    
Thank you, that worked once I fixed the typos. I did not realize that query_posts took the same parameters as WP_Query, and the tax_query parameter is not listed in the query_posts() documentation. Also, sorry, I need 9 more rep before I can vote you up, but I would if I could. :) –  Michael VanDeMar Feb 16 '13 at 4:56
    
It's ok, you don't have to vote up, it is enaught if you mark answer as correct ;) –  david.binda Feb 16 '13 at 10:33
    
Done, and again, thanks! –  Michael VanDeMar Feb 16 '13 at 15:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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