Forums

[resolved] taxonomy.php problem with a WP_Query (28 posts)

  1. gian-ava
    Member
    Posted 1 year ago #

    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"?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  3. gian-ava
    Member
    Posted 1 year ago #

    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…

  4. vtxyzzy
    Member
    Posted 1 year ago #

    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?

  5. gian-ava
    Member
    Posted 1 year ago #

    Thanks for the interest, vtxizzy!

    I used the code from this tutorial here:
    http://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.

  6. gian-ava
    Member
    Posted 1 year ago #

    By Google-ing my case, I found this:
    http://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?

  7. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  8. gian-ava
    Member
    Posted 1 year ago #

    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?

  9. vtxyzzy
    Member
    Posted 1 year ago #

    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>');
    ?>
  10. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  11. gian-ava
    Member
    Posted 1 year ago #

    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?

  12. gian-ava
    Member
    Posted 1 year ago #

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

  13. gian-ava
    Member
    Posted 1 year ago #

    I've created an account with "Editor" privileges.
    Thanks for letting me know if you get it ok.

  14. gian-ava
    Member
    Posted 1 year ago #

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

  15. vtxyzzy
    Member
    Posted 1 year ago #

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

  16. gian-ava
    Member
    Posted 1 year ago #

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

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

  17. vtxyzzy
    Member
    Posted 1 year ago #

    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')!

  18. gian-ava
    Member
    Posted 1 year ago #

    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.

  19. gian-ava
    Member
    Posted 1 year ago #

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

  20. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  21. gian-ava
    Member
    Posted 1 year ago #

    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');
    ?>

  22. vtxyzzy
    Member
    Posted 1 year ago #

    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?

  23. gian-ava
    Member
    Posted 1 year ago #

    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: http://site.com/countries/spain/

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

  24. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  25. gian-ava
    Member
    Posted 1 year ago #

    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?

  26. vtxyzzy
    Member
    Posted 1 year ago #

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

  27. gian-ava
    Member
    Posted 1 year ago #

    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.

  28. vtxyzzy
    Member
    Posted 1 year ago #

    You are very welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic