WP 3.1 getting tax_query to work in query_posts()

I’m trying to filter my posts using multiple custom taxonomies for my custom posts with the following code, but I keep getting blanks with my new code i.e. no posts appear in the loop.

It works like this: the user chooses a term for custom taxonomies ‘fttype’, ‘ftperiod’ and ‘ftduration’ from three different dropdowns in a form, and this is passed to the code below as:

Read More
  • $ft_t_ns
  • $ft_p_ns
  • $ft_d_ns

I originally had this code (which works, albeit without pagination), but the new code I’m trying to implement so that I can use WP 3.1’s in-built capability of filtering by multiple custom taxonomies isn’t working (see my new code further down).

Can anyone see what I’m doing wrong here? Been fighting with this for a while…

Thanks

osu

OLD CODE

// Set todays date to check against the custom field StartEventDate
$todaysDate = date('Y/m/d');

// Convert spaces in taxonomies and terms into hyphens so that search works correctly (uses slug)
$ft_t_ns = osu_convert_spaces($ft_t);
$ft_p_ns = osu_convert_spaces($ft_p);
$ft_d_ns = osu_convert_spaces($ft_d);

// Build query
// NOTE: AS OF WP 3.1, SEE V2 FOR HOW TO PASS AN ARRAY TO query_posts(). YOU PROBABLY WON'T NEED
// QUERY MULTIPLE TAXONOMIES PLUGIN EITHER FOR V2'S APPROACH OF PASSING AN ARRAY TO WP_Query() TO WORK.
// READ MORE ON 'MULTIPLE TAXONOMY HANDLING' HERE:
// http://codex.wordpress.org/Function_Reference/query_posts#Taxonomy_Parameters
$ft_args = 'post_type=ftevent';
$ft_args .= '&fttype=' . $ft_t_ns;
$ft_args .= '&ftperiod=' .$ft_p_ns;
$ft_args .= '&ftduration=' . $ft_d_ns;
$ft_args .= '&posts_per_page=' . $ft_ppp;
$ft_args .= '&meta_key=StartEventDate&meta_compare=>=&meta_value=' . $todaysDate;
$ft_args .= '&orderby=meta_value&order=ASC&paged=' . $paged;

// Create query
query_posts($ft_args);

NEW CODE

$ft_args = array(
    'post_type' => 'ftevent',
    'posts_per_page' => 5,
    'paged' => $paged,
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'StartEventDate',
            'value' => $todaysDate,
            // Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 
            // 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.
            'type' => 'DATE',
            // Operator to test. Possible values are 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
            // We choose 'BETWEEN' because we need to know the date has not passed to show the event
            'compare' => 'BETWEEN'
        )
    ),
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'fttype',
            'field' => 'slug',
            'terms' => $ft_t_ns,
            // Operator to test. Possible values are 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
            // We choose 'IN' because we need to make sure the term is in the current array of posts
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'ftperiod',
            'field' => 'slug',
            'terms' => $ft_p_ns,
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'ftduration',
            'field' => 'slug',
            'terms' => $ft_d_ns,
            'operator' => 'IN',
        ),
    )
);

// Create query
query_posts($ft_args);

Related posts

Leave a Reply

1 comment

  1. $todaysDate needs to be an array of two values to be BETWEEN for the compare.

    For example if you wanted to use a date range from today to a week in the future, then you might use something like..

    $todaysDate = array(
        date('Y/m/d'),
        date('Y/m/d', strtotime('+1 week') )
    );
    

    Or if you wanted to go back in time, then maybe..

    $todaysDate = array(
        date('Y/m/d'),
        date('Y/m/d', strtotime('-1 week') )
    );
    

    Following on from the comments: try this as the meta_query part of your code.

    'meta_query' => array(
        array(
            'key' => 'StartEventDate',
            'value' => date('Y/m/d'),
            'type' => 'DATE',
            'compare' => '>=' // more than or equals
        )
    )
    

    NOTE: It’s still possible to use the old >(more than), <(less than) and other meta comparisons with the new meta_query args, or at least it looks that way by looking directly at the source.

    http://core.trac.wordpress.org/browser/trunk/wp-includes/meta.php#L355

    Hope that helps.