Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Pagination issue with Custom Post Types + Advanced Custom Fields

  • This topic is empty.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #170505
    deeve007
    Participant

    I have a Custom Post Type created where I have basic listing and pagination working fine as per this code more or less: https://css-tricks.com/snippets/wordpress/paginate-custom-post-types/

    The problem comes about when I add this filter for what posts to display, which checked an Advanced Custom Fields field I have attached to the CPT:

    $myargs = array (
              'post_type'   => 'projects',
              'paged'       => $paged,
              'meta_key'    => 'archive_item',
              'meta_value'  => 'No'
          );
    

    The relevant posts are excluded from displaying, however the pagination acts as if they’re still there, so for example if each page is set to show 5 posts, then even if only 5 posts are displaying as per the meta_key stipulated, pagination still links to a second page, where no posts are displayed.

    Anyone know how to fix this?
    (or if my description isn’t clear please let me know)

    #170572
    chrisburton
    Participant

    I don’t use WP but have you solved this or still need help?

    #170599
    deeve007
    Participant

    Nope, if I’d solved it I would come back and post the solution.

    #170606
    chrisburton
    Participant

    @deeve007 Well, did you try the archives-{post-type-name}.php method?

    #170607
    deeve007
    Participant

    The post types are all set up to display using relevant templates for both the archives and the single posts. The issue is that the pagination is not able to recognise the correct number of items the loop has returned via the arguments used.

    #170608
    chrisburton
    Participant

    Can I see your CPT loop?

    #170613
    deeve007
    Participant
    <?php
    // Loop filtering by ACF field
    wp_reset_postdata();
    $myargs = array (
      'post_type'   => 'consultations',
      'paged'       => $paged,
      'orderby'     => 'title',
      'order'       => 'ASC',
      'meta_key'    => 'archive_item',
      'meta_value'  => 'No'
    );
    $myquery = new WP_Query($myargs);
    if($myquery->have_posts() ) :
      while($myquery->have_posts() ) : $myquery->the_post();
    ?>
    
    <!-- Do you html stuff here -->
    
    <?php
    endwhile;
    endif;
    wp_reset_postdata();
    ?>
    
    <?php if (show_posts_nav()) : ?>
    <?php if ( get_previous_posts_link() ) { ?>
        <span>&lt;?php previous_posts_link('&laquo; Newer consultations') ?&gt;</span>
    &lt;?php } ?&gt;
    &lt;?php if ( get_next_posts_link() ) { ?&gt;
        <span>&lt;?php next_posts_link('Older consultations &raquo;') ?&gt;</span>
    &lt;?php } ?&gt;
    &lt;?php endif; ?&gt;
    
    #170614
    chrisburton
    Participant

    Where is $paged coming from?

    #170615
    deeve007
    Participant

    It’s an inbuilt variable in WordPress.

    #170616
    chrisburton
    Participant

    What is annoying is that this seems to be a common issue but at the same time there are so many different answers around the web.

    #170617
    deeve007
    Participant

    I haven’t found one that makes sense and/or works as yet.

Viewing 11 posts - 1 through 11 (of 11 total)
  • The forum ‘Back End’ is closed to new topics and replies.