0

I have an event loop and I'm wondering if it's possible to offset the loop by -1, so the prev and next posts of the current post are shown.

EDIT - Thanks for your comments, I think I can clarify. When a visitor is on a single-event page, I want to show 4 events at the bottom of the page; the first 2 being past events and the next 2 being upcoming events. I think I need to query all of the events, get the position of the current event result, subtract 2 from that number, and apply that number to the offset.

$upcoming_events_args = array(
    'post_type' => 'event',
    'meta_key' => 'start_date', // name of custom field
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'posts_per_page' => '3',
    'meta_query' => array(
        array(
            'key' => 'start_date',
            'value' => $date_of_page,
            'compare' => '>=',
            'type' => 'DATE'
        )
    )
);
3
  • 3
    When you have a problem, explain it and ask for a solution. If you invent an odd solution and than ask how can you make that solution work, understing your problem, and so give you a solution, became hard.
    – gmazzap
    Aug 19, 2013 at 22:35
  • 1
    There is a sense in which this question doesn't make sense-- if you are on the first result there is no "-1". Results start at zero. Please edit the question so it is more clear what you want.
    – s_ha_dum
    Aug 19, 2013 at 23:30
  • 1
    Importantly, explain why
    – Tom J Nowell
    Nov 25, 2014 at 3:22

1 Answer 1

1

I couldn't understand your question. exactly

  • Do you want next, prev post pagination?

    or

  • You want to see next and prev posts titles or other content?

Actually you placed an incorrect value to offset. You have to do something like this.

// Initialize where to start the post from, 0 is most recent post
$init_count = 0;

// Get the current page integer
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

// And the simple formula for offset is this
$offset = ( $page - 1 ) * $init_count;

//Now you should use your query like this:

<?php $upcoming_events_args = array(
    'post_type' => 'event',
    'meta_key' => 'start_date', // name of custom field
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'posts_per_page' => '3',
    'number'     =>  $init_count,
    'page'       =>  $page,
    'offset'     =>  $offset,
    'meta_query' => array(
    array(
        'key' => 'start_date',
        'value' => $date_of_page,
        'compare' => '>=',
        'type' => 'DATE'
      ))
    );

And by placing this code after end of the loop, will show your next and previous post.

<ul class="pagination">
    <li id="previous-posts">
        <?php previous_posts_link( '<< Previous Posts', $custom_query->max_num_pages ); ?>
    </li>
    <li id="next-posts">
        <?php next_posts_link( 'Next Posts >>', $custom_query->max_num_pages ); ?>
    </li>
</ul>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.