• Resolved soyelnacho

    (@soyelnacho)


    Hello all

    Im having a problem while trying to create a template.

    In home, i have a loop, who shows the 10 last post published.

    Now, I need to sort from oldest to newest, but only display events that have not happened.

    I mean, it’s for a events blog, I’m using the plugin “No Future Posts”, which allows to set the date of publication of the posts in the future, but mark them as published prior to the date.

    The site is at: https://www.tocatas.de (white with orange boxes in the home).

    I have the following code:

    <?php
        $my_query = new WP_Query('showposts=10&cat=-26');
        if ($my_query->have_posts()) : while ($my_query->have_posts()) :
            $my_query- >the_post();
            $do_not_duplicate = $post- >ID;
    ?>
    
    <?php
        //do not show older posts
        $expiredate = get_the_time('m/d/Y')." 00:00:00";
        $secondsto = strtotime($expiredate)-time();
        if ( $secondsto > 0 ) {
        ? >
    
            < div class="homeevento" >
            < ?php the_title(); ? >
            ... (the post)
            < /div >
    
            < ?php } //expiredate ? >
            < ?php endwhile; else: ? >
            < ?php _e('No upcoming Events'); ? >
            < ?php endif; ? >

    So now the code shows the posts that have not happened (those who have not reached their date of publication), ignoring all others. But this showed me first the more distant, then the closer. I need to show first those ones who are closest to the current date.

    When you modify the query by placing order=ASC, to show what I need, does not print anything on the screen. I tried using something like:
    if ($ segundosquefaltan> 0): continue to see if that continued the loop of 10 posts, but neither worked.

    I hope you can help me, and hope you can understand my poor english

    THX!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Not sure why you have a problem…try this for you query:

    Get 10 posts, exclude category 26, exclude stickies, sort by date in ascending (oldest date first) order,

    $args = array(
    'cat' => -26,
    'showposts'=>10,
    'caller_get_posts'=>1,
    'post_type' => 'post',
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'ASC'
    );
    $my_query = new WP_Query($args);

    Thread Starter soyelnacho

    (@soyelnacho)

    damn! did not work for me!

    try to explain it better.

    suppose today’s date is: October 13.
    I have in my database the following posts:

    * Post #1 (January 20)
    * Post #2 (March 15)
    * Post #3 (April 20)
    * Post #4 (May 5)
    * Post #5 (May 10)
    * Post #6 (June 5)
    * Post #7 (June 10)
    * Post #8 (July 6)
    * Post #9 (August 10)
    * Post #10 (September 20)
    * Post #11 (October 5)
    * Post #12 (October 15) <- must appear
    * Post #13 (November 3) <- must appear
    * Post #14 (November 10) <- must appear
    * Post #15 (November 16) <- must appear

    then, when I do the loop,
    saying the oldest must be listed first (post 12 first then post 13, then post 14, then post 15) the loops stats trying to print post 1, then post 2, then post 3, and so untill the post 10.

    The problem is, my first 10 post, are all from past date, so nothing is printed.

    Hope you can help me again!

    THX!

    Might look at the examples in the query_posts article.

    Thread Starter soyelnacho

    (@soyelnacho)

    wow! it works!

    thanks!

    now, i have another problem…

    in the same page, after these query, i have another query, to show only one category posts…

    after added the new code, the seconds loops, didnt fin any post in these category, im receiving a ugly “No upcoming Events”.

    The code added to the first query:

    <?php
    function filter_where($where = '') {
    $where .= " AND post_date > '" .date('Y-m-d') . "'";
    return $where;
    }
    add_filter('posts_where', 'filter_where');
    
    $args = array(
    'cat' => -26,
    'showposts'=>10,
    'post_type' => 'post',
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'ASC'
    );
    $my_query = new WP_Query($args);
    
    if ($my_query->have_posts()) : while ($my_query->have_posts()) :
    $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>
    
               ...posts code...
    
    <?php endwhile; else: ?>
    <?php _e('No upcoming Events'); ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    and, in mi second loop:

    <?php
    $my_query = new WP_Query('category_name=entrevistas&showposts=1');
    if ($my_query->have_posts()) : while ($my_query->have_posts()) :
    $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>
    
            <?php the_title(); ?>
    
    <?php endwhile; else: ?>
    <?php _e('No upcoming Events'); ?>
    <?php endif; ?>

    Can you see the problem there?

    Thx!!

    Assuming that if you just use the 2nd loop (without the first) that it does return posts, then might need to use remove_filter(‘posts_where’, ‘filter_where’) before that 2nd loop.

    I was going to suggest the same, remove the filter after it’s used..

    Or just put some conditions inside your posts_where function, so the filter is only applied where it’s needed, for example if you wanted to do it on the home page (purely example).

    function filter_where($where = '') {
    if($query->is_home) $where .= " AND post_date > '" .date('Y-m-d') . "'";
    return $where;
    }

    Thread Starter soyelnacho

    (@soyelnacho)

    great!
    works prefect!

    thank you very much MichaelH for you help and patient.

    thanks t31os_ too!

    ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘order posts from older to newer with custom loop, and excluding ones’ is closed to new replies.