Home Forums General Issues Query posts by custom fields Pagination not Working


Query posts by custom fields Pagination not Working

  • hi
    The idea: i want to show posts with normal pagination based on user role and ACF
    Ex. if the user has a role of “student” only show posts with the checkbox student checked in.

    My code so far:

    $user_roles = array(); //initiate an empty user role array
    foreach($current_user->roles as $key => $value) {
       array_push($user_roles, $value);
       // query the posts by custom field only for non admin user roles
       if(!in_array('administrator', $user_roles)) {
       $meta_query = array('relation'=> 'OR');
       foreach($user_roles as $role){
        	array_push($meta_query, array(
    		'key' => 'role_access',
    		'value' => $role,
    		'compare' => 'LIKE'
       $args['meta_query'] = $meta_query;
       $q = new WP_Query($args);
       if ($q->have_posts()) :
          while ( $q->have_posts() ) : $query->the_post();
             normal custom loop code
             default wordpress pagination
          endif; wp_reset_postdata();

    OK: everything works fine so far without pagination
    Behavior: pagination gets the same posts displayed on page 1 or gets a 404

    The general idea is to display posts by user role, tried the members plugin, but only restricts content, i dont want to display the title if the user has no acces to that post.

  • Hi,

    If you are doing a secondary on a page, you’d need to pass the pagination argument into your WP_Query.

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $query = new WP_Query( array( 'paged' => $paged ) );


Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Query posts by custom fields Pagination not Working’ is closed to new replies.