1

I'm bit confused about paginating the results generated from an array in WordPress. I've a couple of event Ids per user that I'm saving as a string: -

id | userid | eventids
1  |  22    |  33,45,554, 225

I fetch these results using $wpdb->get_var ($query), for which I get a string "33,45,554,225". I'm then converting this string into an array so that I can process it further.

So, $eventsList = explode ( ",", $wpdb->get_var($query) );

I then fetch the event titles using following loop:

foreach ( $eventsList as $event ) {
 echo get_the_title ($event); 
}

Now the problem is that I do not know how to paginate these results. I've the following standard code that generates the pagination links, but each page shows the same data.

$big = 999999999; // need an unlikely integer
$countOfEvents        = count( $eventsList );
$page_count      = ceil($countOfEvents / $eventsPerPage );

$employer_dashboard_pagination_args = array(

    'base'  => str_replace( $big, '%#%', esc_url (get_pagenum_link( $big )) ),
    'format' => '/page/%#%',
    'current'   => $paged,
    'total' => $page_count,
    'prev_text' => '« Previous',
    'next_text' => 'Next »'
);

Would really appreciate if someone could help fixing this problem.

2
  • Explode returns a Key=>name array. Does your event title foreach loop works ? Commented Oct 16, 2016 at 8:10
  • Yes, it does return ( '0' => 33, '1' => 45, '2' => 554, '3' => 225 ) , which I iterate. So I can actually display a list of events, but not able to paginate them. Commented Oct 16, 2016 at 9:12

1 Answer 1

0

Okay! I fixed it all by myself. Adding it here so that it can help others looking for a solution.

For all things 'posts' (and post types), WP_QUERY is your friend. Do go through all the arguments it accepts. I found out that the parameter 'post__in' accepts the IDs of the posts and then fetches those posts for you. You can paginate them easily.

I simply pass my $eventList array to 'post__in' parameter and it does what I was trying to do.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.