WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP_Query to display the_meta from older posts (3 posts)

  1. rakemyleaves
    Member
    Posted 3 years ago #

    Hey everyone,
    Long-time reader, first time poster.
    I created a custom post type called 'profentia'. In this custom post type I created custom fields to hold data. I want each single 'profentia' post to display its data as well as the previous 4 post's data. Currently, all posts display the 5 most recent days of data. How do I tell my new loop to start with the current post and go backwards? Let me know if more info is needed. Thanks in advance :)

    This is the code I am using for my second loop:
    <?php
    $my_query = new WP_Query('post_type=profentia', 'showposts=5');
    if ( $my_query->have_posts() ) {
    while ( $my_query->have_posts() ) {
    $my_query->the_post();
    ?>
    <div class="inline-list-data"><?php the_meta(); ?></div><?php
    }
    }
    wp_reset_postdata();
    ?>

  2. vtxyzzy
    Member
    Posted 3 years ago #

    Take a look at the Codex here to see how to add a filter to a query.

    If you have the current post date in $current_date and current ID in $current_id, your 'where' clause should look something like this:

    $where .= " AND post_date <= '$current_date' AND ID != $current_id ";

  3. rakemyleaves
    Member
    Posted 3 years ago #

    Thanks for the reply. You are correct, this is exactly what I needed to do. I found this solution before your post and since I am not really a programmer I hacked my way through using this:

    $days = 12 * 86400;
    $p_time = get_the_time('U') + 86400;
    $back_time = date('Y-m-d',($p_time - $days ));
    $post_time = date('Y-m-d',($p_time));
    
    function filter_where($where = '') {
    	global $back_time, $post_time;
    	$where .= " AND post_date >= '$back_time' AND post_date < '$post_time'";
      return $where;
    }
    add_filter('posts_where', 'filter_where');

    Yours look cleaner, but mine is working so I am leaving it as is for now. Thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags