Results 1 to 8 of 8

Thread: Custom post types - limiting to specific taxonomies

  1. #1
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default Custom post types - limiting to specific taxonomies

    Any ideas why this works (with either red or blue as the colour):
    PHP Code:
    $loop = new WP_Query(
        array(
            
    'colour'          => 'red',
            
    'post_type'       => 'products'
            
    'posts_per_page'  => 12,
        )
    ); 
    But when I change the colour to an array to allow me to load two options at the same time, it doesn't load anything at all:
    PHP Code:
        $loop = new WP_Query(
        array(
            
    'colour'          => array( 'red''blue' ),
            
    'post_type'       => 'products'
            
    'posts_per_page'  => 12,
        )
    ); 
    This is driving me a little batty :(


    An alternative solution would be to load all the posts, then skip them via some sort of conditional, but I'm not sure what that conditional would look like. Any tips on this would be much appreciated too :)

  2. #2
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

  3. #3
    Utkarsh is offline Hello World
    Join Date
    Nov 2009
    Posts
    73

    Default

    Quote Originally Posted by Ryan View Post
    Any ideas why this works (with either red or blue as the colour):
    PHP Code:
    $loop = new WP_Query(
        array(
            
    'colour'          => 'red',
            
    'post_type'       => 'products'
            
    'posts_per_page'  => 12,
        )
    ); 
    But when I change the colour to an array to allow me to load two options at the same time, it doesn't load anything at all:
    PHP Code:
        $loop = new WP_Query(
        array(
            
    'colour'          => array( 'red''blue' ),
            
    'post_type'       => 'products'
            
    'posts_per_page'  => 12,
        )
    ); 
    This is driving me a little batty :(


    An alternative solution would be to load all the posts, then skip them via some sort of conditional, but I'm not sure what that conditional would look like. Any tips on this would be much appreciated too :)
    WordPress doesn't currently support querying taxonomies like that. You will have to run some custom sql query (not sure what that would be).

    Code:
    SELECT * 
    FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
    WHERE $wpdb->posts.post_type = 'products' 
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->term_taxonomy.taxonomy = 'colour'
    AND $wpdb->terms.slug = 'red' OR $wpdb->terms.slug = 'blue'
    ORDER BY $wpdb->posts.post_date DESC

  4. #4
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

  5. #5
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    This plugin might help (I still didn't play with taxonomies myself, so theoretically speaking :)
    http://scribu.net/wordpress/query-multiple-taxonomies

    PS every time I see Oh crap! reaction I feel like loudly shouting "THIS IS WORDPRESS!!!" (Sparta-style) :)
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  6. #6
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default

    At least I know I'm not losing my marbles. I was seriously starting to doubt my sanity trying to get this annoying thing to work.

  7. #7
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default

    Quote Originally Posted by Rarst View Post
    This plugin might help (I still didn't play with taxonomies myself, so theoretically speaking :)
    http://scribu.net/wordpress/query-multiple-taxonomies
    That looks like it's a widget. Maybe the internal code could be useful, but it looks a little spaghetti-ish to me (code comments would probably help :P ).

  8. #8
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    Quote Originally Posted by Ryan View Post
    That looks like it's a widget. Maybe the internal code could be useful, but it looks a little spaghetti-ish to me (code comments would probably help :P ).
    Widget is just a bonus. And yes, Scribu's code takes some effort to decipher. :)
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •