• 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 12 replies - 16 through 27 (of 27 total)
  • vtxyzzy

    (@vtxyzzy)

    Well, there seems to be a problem because there are no dates of ‘today’ in the result. So, you would not find any posts that match date(‘d/m/Y’)!

    Thread Starter Gianfranco

    (@gian-ava)

    Mm… well, the point is that I need to dispaly posts with a date “newer” than “today”, because it’s events to happen. If the date matches “today” or is older, than they don’t need to show up. That’s the whole point.

    Thread Starter Gianfranco

    (@gian-ava)

    Oh, and they need to be sorted by “their date” not the “WP publish date”.

    vtxyzzy

    (@vtxyzzy)

    That sheds new light on the problem. But, it creates a new problem because, as I said before, you can’t sort (or compare) dates in the format ‘d/m/Y’. You will need to rearrange the dates into ‘Y/m/d’ format and then adjust your query a little.

    <?php
    $todaysDate = date('Y/m/d');// 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');
    ?>

    Notice that I changed the date format and took out the equal sign in the compare.

    Thread Starter Gianfranco

    (@gian-ava)

    Ok, vtxyzzy, they’re showing up now!
    ??

    Only thing is, the” taxonomy.php” template gets all the posts. It does not filter by terms when you click on a “taxonomy term”.

    Back to the original problem (with a good code, at least).

    The code for taxonomy.php is the same as the one in the index:


    <?php
    $todaysDate = date('Y/m/d');// 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');
    ?>

    vtxyzzy

    (@vtxyzzy)

    There is nothing in the code you show to select a taxonomy term. Where do you ‘click on a taxonomy term’? What is the URL that is created?

    Thread Starter Gianfranco

    (@gian-ava)

    Ok, I got two Custom Taxonomies registered for the “Agenda” post type.

    1) martial_arts
    2) countries

    The “countries” taxonomy has, so far, these tems: Belgium, Spain
    The “martial_arts”, these: jeet kune do, silat, kali (knife), kali (stick), mma

    When you are in the”Agenda” index and I click on a taxonomy term, I get for example an URL like: https://site.com/countries/spain/

    But all the Custom Posts shows, not only the ones with the “spain” taxonomy term.

    vtxyzzy

    (@vtxyzzy)

    I think the problem is that you are ‘throwing away’ the query created by clicking on the taxonomy term and replacing it with your agendaloop.

    You need to keep the original query arguments and add yours to them, something like this:

    <?php
    $todaysDate = date('Y/m/d');// Get today's date in the right format
    $myargs = array(
       'post_type' => 'agenda',
       'posts_per_page' => 10,
       'meta_key' => '_date',
       'meta_compare' => '>',
       'meta_value' => $todaysDate,
       'orderby' => 'meta_value',
       'order' => 'ASC');
    global $wp_query;
    $agendaloop = new WP_Query(array_merge( $wp_query->query, $myargs ));
    ?>

    I obviously don’t have a way of testing this, but it should be close.

    Thread Starter Gianfranco

    (@gian-ava)

    vtxyzzy, that did it!
    ??

    It all works as I intended to, now. I would have not find this by myself, thanks a lot.

    Now, I don’t mean to abuse of your precious help, but is there a way to get a better format for the date?

    Ideally, I’d like to show a date such as ” 3 February 2011″ instead of 2011/02/03.

    Of course, I could have two custom fields, one withthe sort date, to sort the posts, and the other with the user-friendly date, for easy scanning. But at some point I’d like to have users to suggest an event with a form and register it in the backend, for the Admin to validate and publish, and it’d be better to auto-generate user-friendly date (or the other way around).

    Is there a way to do that?

    vtxyzzy

    (@vtxyzzy)

    You can format the date using the strtotime and date functions. If the date is in $post->meta_value, you can format it like this:

    $display_date = date('F jS, Y',strtotime($post->meta_value));

    If this completes your request, please use the dropdown at top right to mark this topic ‘Resolved’.

    Thread Starter Gianfranco

    (@gian-ava)

    Thanks a lot for the help.
    Unfortunately I got no time left to test this out (the date), but I may post another topic for this and discuss it f needed, since this is not in the inital scope of this topic.

    I’ve changed the status to Resolved.
    Again, thanks, vtxyzzy. You made my day. I appreciate it greatly.

    vtxyzzy

    (@vtxyzzy)

    You are very welcome!

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