WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. steveniweiss
    Member
    Posted 3 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');
    endwhile;
    ?>`

    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. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    the date as a variable:

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

    http://codex.wordpress.org/Function_Reference/the_date

    rearranged to get '2010 11 26' into the string;
    http://codex.wordpress.org/Formatting_Date_and_Time

    query_posts() to get posts of a certain date:

    http://codex.wordpress.org/Function_Reference/query_posts#Time_Parameters

    using your $issuedate:

    $issue_datum = explode(' ',$issuedate);
    query_posts('year=' . $issue_datum[0] .'&monthnum=' . $issue_datum[1] .'&day=' . $issue_datum[2] );
  3. steveniweiss
    Member
    Posted 3 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

    if(in_category(135))
    inside the while have_posts() loop?

  4. steveniweiss
    Member
    Posted 3 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. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

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

    http://codex.wordpress.org/Function_Reference/wp_reset_query

Topic Closed

This topic has been closed to new replies.

About this Topic