• Resolved Gianfranco

    (@gian-ava)


    I am building an Event section for a website, and using custom taxonomies to sort the events list (by “country” and “martial arts styles”).

    The query does it job in ordering the envents by “event date” and not by “published date” for the main list ‘index (called “agenda.php”). But when you click on a “taxonomy term”, the query doesn’t filter as on the “agenda.php”. I got a “taxonomy.php” template file in which I pasted the same loop than on the “agenda.php”. The posts display, but not in the right order.

    Here’s my loop:


    <?php
    $todaysDate = date('d/m/Y');
    $agendaloop = new WP_Query('post_type=agenda&posts_per_page=10&meta_key=_date&meta_compare=>=&meta_value=$todaysDate&orderby=meta_value&order=ASC');
    ?>
    <?php while ( $agendaloop->have_posts() ) : $agendaloop->the_post(); ?>

    [stuff here…]

    <?php endwhile; ?>

    Is there anybody who can figure out why the query doesn’t affect “taxonomy.php”?

Viewing 15 replies - 1 through 15 (of 27 total)
  • It looks like you are selecting all posts with a meta_value equal to $todaysDate and then sorting on that same meta_value. Since all posts have the same meta_value, there is nothing unique to sort them in a certain order. Try using something else for orderby.

    Thread Starter Gianfranco

    (@gian-ava)

    If I delete the “orderby” I get the same result: all posts shows instead of just the one with the taxonomy term.

    I wouldn’t know another method how to sort posts by date (custom field) instead than default “publish date”.

    Mmm…

    I think there is another problem in the query – it is enclosed in single quotes. Therefore the value for $todaysDate is never inserted into the query string. Is this correct?

    Thread Starter Gianfranco

    (@gian-ava)

    Thanks for the interest, vtxizzy!

    I used the code from this tutorial here:
    https://www.problogdesign.com/wordpress/how-to-make-a-wordpress-events-list/

    Now, whne I tried this:

    <?php
    $todaysDate = date('d/m/Y');// Get today's date in the right format
    $agendaloop = new WP_Query('post_type=agenda&posts_per_page=10&meta_key=_date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');
    ?>

    … no post are showing!
    If don’t use the dot before the $todaysDate at least the posts shows in the index page and get listed as intended.
    If that is what you meant.

    Thread Starter Gianfranco

    (@gian-ava)

    By Google-ing my case, I found this:
    https://wordpress.stackexchange.com/questions/3542/custom-taxonomy-template-post-list-with-sort-order

    Which is the very exact problem I am having.
    I guess he solved it, but the code they are using in there looks much more complicated to get posts ordered by “custom filed date”.

    Any thought on that?

    The code with the dots is the correct way.

    <?php
    $todaysDate = date('d/m/Y');// Get today's date in the right format
    $agendaloop = new WP_Query('post_type=agenda&posts_per_page=10&meta_key=_date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');
    ?>

    My guess would be that either the format of the date does not match what is in the meta_value or that there are no posts with the correct date in the meta_value.

    Thread Starter Gianfranco

    (@gian-ava)

    Yeah, I’m sure the code with the dots is the good one.

    But than, why do I get exactly what I want with the one without dots?
    (Meaning custom post types showing by “custom field date”).

    When I use the one WITH the dots, I don’t get anything from the query. And I am preatty sure the dates are correctly coded in the custom field, otherwise I shouldn’t get the it right when using the code WITHOUT dots.

    I hope I’m not confusing you too much with this.
    ??

    So what else is worthing trying?

    I really doubt that you are getting ‘exactly what I want’ from the query without the dots. What I think is happening is that the query is ignoring everything after the $todaysDate entry in that query.

    As a side note, you cannot sort on a date in ‘d/m/Y’ format. To sort correctly, dates must be in ‘Y/m/d’ format. But, since you sre selecting a single date, the sort order is moot.

    The code with the dots is certainly the correct format, so we need to find out why it is not finding the posts.

    Can you insert the following debugging code before your loop and post a few lines of the output (say 10 – 15 lines)?

    <?php
    global $wpdb;
    $dates = $wpdb->get_col("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = '_date' ");
    print_r('<p>');print_r($dates);print_r('</p>');
    ?>

    Would you be willing to add an admin login for me in your site so I can look at things first-hand?

    If so, set up a temporary login that you can delete when we are done. Use an email of mac =at= mcdspot =dot= com. The password should be mailed to me by the registration process.

    Thread Starter Gianfranco

    (@gian-ava)

    One thing that I should add is that I actually used Custom Meta Boxes to define Custom Fields withni the Post Type “Agenda”.

    I noticed that in the even thought Custom Fields are in the Meta Box, they do not appear on the “Custom Fields Panel” itself. Is this the way this should normally work? Or there’s a hint in that?

    Thread Starter Gianfranco

    (@gian-ava)

    Ok, I am using the “dots” version now, since this is the correct code. We’ll take it from there.

    Thread Starter Gianfranco

    (@gian-ava)

    I’ve created an account with “Editor” privileges.
    Thanks for letting me know if you get it ok.

    Thread Starter Gianfranco

    (@gian-ava)

    You go in the “Agenda” section. Some test posts are in there, set to “private”. They’re not public.

    You may as well delete the account – Editor privileges will not let me look at the code or do any debugging. Thanks anyway.

    Thread Starter Gianfranco

    (@gian-ava)

    Ok, this is the result from the debugging code you provided:

    Array ( [0] => 15/12/2010 [1] => 10/12/2010 [2] => 05/12/2010 )

Viewing 15 replies - 1 through 15 (of 27 total)
  • The topic ‘taxonomy.php problem with a WP_Query’ is closed to new replies.