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'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:

  • $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);
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

$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.

share|improve this answer
    
@t31os - Thanks for this, do you know how I'd compare $todaysDate (Y/m/d) and my Custom Field 'StartEventDate' (which is a date written as YYYY/MM/DD) within my $ft_args? –  Osu Feb 28 '11 at 23:32
    
@Osu - Please see updated answer. –  t31os Feb 28 '11 at 23:45
    
@t31os - that's great, seems to have sorted out the ordering and display of custom posts when you first visit the page. Thanks for all your help so far. I'm now trying to figure out why when I submit my form that I get a 404! You've helped me loads already, but if you have the time, it would be great to hear your opinion on why you think that might be happening as you seem to be a Wordpress 'ninja' so to speak - here's my code in pastie for you or anyone interested: pastie.org/1618788 –  Osu Feb 28 '11 at 23:52
    
Maybe the form isn't always submitting to the correct URL? –  t31os Mar 1 '11 at 1:14
1  
I'd personally trim down your whole argument building routine to something like this. Can you follow what i've done there alright? –  t31os Mar 2 '11 at 17:49
show 18 more comments

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.