Storing a Post's Date in A Variable, Then Using That Variable in query_posts (5 posts)

  1. steveniweiss
    Posted 5 years ago #

    I did a long-winded explanation here(http://wordpress.org/support/topic/creating-an-issue-date-algorithm-that-can-work-in-outside-the-loop?replies=1), but, basically, how can I get a date one post was published stored in a variable, and then use that variable in future loops?

    I've made a few attempts using the_date() and post_date, add_filter, and other means, but can't seem to get the format of the date in the variable to work with the query_posts.

    Here's one very simple approach I've taken to get the date stored in the variable:

    `<?php query_posts('orderby=date&cat=135&showposts=1'); ?>
    <?php while (have_posts()) : the_post();
    $issuedate = the_date('F j Y');

    But I can't seem to get that date information to work with query_posts to return only posts with the same publishing date.

  2. Michael
    Part-Time Forum Moderator
    Posted 5 years ago #

    the date as a variable:

    $issuedate = the_date('Y n j','','',false);


    rearranged to get '2010 11 26' into the string;

    query_posts() to get posts of a certain date:


    using your $issuedate:

    $issue_datum = explode(' ',$issuedate);
    query_posts('year=' . $issue_datum[0] .'&monthnum=' . $issue_datum[1] .'&day=' . $issue_datum[2] );
  3. steveniweiss
    Posted 5 years ago #

    Thanks! That's working great!

    One question: Any idea why I'm getting posts from more than just the $issuedate if I refine The Loop further by adding

    inside the while have_posts() loop?

  4. steveniweiss
    Posted 5 years ago #

    Actually, it seems the problem I'm having is when running a similar query twice on one page.

    If I make two sets of variables to run two different queries in which to get two different post dates stored, the subsequent query_posts relying on the second set of variables doesn't seem to restrict the date.

  5. Michael
    Part-Time Forum Moderator
    Posted 5 years ago #

    maybe adding a line with wp_reset_query(); after each of your queries can help.


Topic Closed

This topic has been closed to new replies.

About this Topic