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.

I have a query based on multiple post_types and taxonomies that works well, however I would like to add a level of complexity and query a taxonomy only for a specific taxonomy.

This query works as expected:

Array
(
    [topic] => global-reach
    [sections] => programs-and-services
    [regions] => ghana
    [post_type] => Array
        (
            [0] => publications
            [1] => media_releases
            [2] => page
        )
    [posts_per_page] => 10
)

Now I would like to select only the 'pages' that have the taxonomy 'options' set to 'set-as-news-item' ... here is a somewhat unlogical pseudo query of what I would like to accomplish, but it should get the idea across.

Array
(
    [topic] => global-reach
    [sections] => programs-and-services
    [regions] => ghana
    [post_type] => Array
        (
            [0] => aucc_publication
            [1] => aucc_media
            [2] => Array 
                     (
                       [post_type] => 'page',
                       [options] => 'set-as-news-item'
                     (
        )
    [posts_per_page] => 10
)

I could use the first query above and filter out unwanted posts as I loop them, but then I could not use Wordpress' built-in pagination.

I'm asking for too much out of WP_Query right?

share|improve this question
    
You can't query pages with a specific term but other types without it, not within the same query, the query class just can't do queries with that level of complexity.. (at least not at the moment).. –  t31os Mar 9 '11 at 15:52
    
That's what I was afraid of. Is my only option to resort to direct SQL queries? –  Bundarr Mar 9 '11 at 16:12

1 Answer 1

As t31os said, you can't do that using WP_Query directy.

You can attempt to use the 'posts_clauses' filter to modify the individual SQL clauses generated by WP_Query, but it will be quite tricky, however you do it.

Instead, it might be easier to replace the entire SQL query. You can use get_tax_sql() to take care of the taxonomy part (not tested):

function my_posts_request( $old_query, $wp_query ) {
    global $wpdb;

    $ptypes = $wp_query->get('post_type');

    if ( 'page' != $ptypes[2]['post_type'] )
      return $old_query; // This is not the WP_Query you are looking for

    // Do the conditional tax_query
    $tax_query = array(
      array(
        'taxonomy' => 'options',
        'terms' => array( 'set-as-news-item' ),
        'field' => 'slug',
      )
    );

    $clauses = get_tax_sql( $tax_query, $wpdb->posts, 'ID' );

    $join = "FROM $wpdb->posts {$clauses['join']}";
    $where = "WHERE (
      post_type = 'aucc_publication' OR 
      post_type = 'aucc_media' OR 
      (post_type = 'page' {$clauses['where']})
    )";

    // Do the other tax query
    $tax_query = array(
      array(
        'taxonomy' => 'section',
        'terms' => array( 'programs-and-services' ),
        'field' => 'slug',
      ),
      array(
        'taxonomy' => 'regions',
        'terms' => array( 'ghana' ),
        'field' => 'slug',
      )
    );

    $clauses = get_tax_sql( $tax_query, $wpdb->posts, 'ID' );

    $join .= $clauses['join'];
    $where .= $clauses['where'];

    return "SELECT $wpdb->posts.* $join $where ORDER BY post_date LIMIT 10";
}
add_filter( 'posts_request', 'my_posts_request' );

You'll still have to take care of pagination etc. Take a look at how WP_Query does it.

share|improve this answer
1  
"This is not the WP_Query you are looking for" - Your jedi mind tricks won't work on me young padawan!... +1 just for the Jedi reference(even if i'm the only one that noticed)... –  t31os Mar 9 '11 at 23:02
    
+1 yourself. I didn't think anyone would catch it :) –  scribu Mar 9 '11 at 23:43
    
Thanks- I have yet to give this a try ... will post results later on –  Bundarr Mar 15 '11 at 18:37

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.