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.

  • Explode returns a Key=>name array. Does your event title foreach loop works ? – Benoti Oct 16 '16 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. – TheBigK Oct 16 '16 at 9:12
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, privacy policy and cookie policy

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