0

I want to make a pager for a custom wp_query where I can have a prev and a next. I have a CPT called family and I must show the families that meet the condition that their status is Retained, important I must show only one family per page, and I must put the paginator to navigate one by one.

This is my code:

$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$args_families = array (
  'post_type'      => array( 'family' ),
  'paged'          => $paged,
  'orderby'        => 'ASC',    
  'posts_per_page'         => 1,    
  'meta_query' => array(
      array(
          'key' => 'status',
          'value' => 'Retained',
          'compare' => '=',
      )
    )
 );
$family_query  = new WP_Query( $args_families );
<?php if ( $family_query->have_posts() ) : ?>
<?php while ( $family_query->have_posts() ) : ?>
    <?php $family_query->the_post(); ?>

    <?php
        echo get_field('status'). '<br>';
        echo ' '.'fid_'.get_field('family_id') . '<br>';
        echo 'ID + ACF'.' '.'fid_'.get_field('family_id' , $family_query->posts[$i]->ID);
        $f_link = 'fid_'.get_field('family_id' , $family_query->posts[$i]->ID);

    ?>
    <h3> <?php echo esc_attr( get_the_title() ); ?></h3>

    <?php $profile_pic = get_field( 'profile_picture' ); ?>
    <img src="<?php echo esc_url( $profile_pic['sizes']['family_large_thumb'] ); ?>" width="300px" height="300px" style="display:block;" />


    <!-- attempt 1 to create the pager -->

    <a href="https://localhost/adoptionnetwork/adopting-families/fid_20074/">next1</a>
    <a href="https://localhost/adoptionnetwork/adopting-families/<?php echo $f_link ?>">next2</a>


    <?php 
    //attempt 1 to create the pager  
    $older_link = '<a href="/adoptionnetwork/adopting-families/fid_' . ( $paged - 1 ) . '" rel="prev"><</a>';
    $newer_link = '<a href="/adoptionnetwork/adopting-families/fid_' . ( $paged + 1 ) . '" rel="next">></a>';       
    ?>

<?php endwhile; ?>

<?php
// Clean up after the query and pagination.
wp_reset_postdata();
?>

But I have no idea how to create the pager for this case.

NOTE: NOTE: I already used these functions but they didn't work.

get_next_post_link( '%link', '<i class="far fa-angle-right"></i>' );
get_previous_post_link( '%link', '<i class="far fa-angle-left"></i>' );

previous_post_link(); 
next_post_link(); 

Thanks for you help

4
  • Are you sure that the people seen in the photo want to be displayed on this site? Just asking. Maybe you can replace the image, obfuscate it or describe in words what your problem is?
    – kaiser
    Dec 15, 2021 at 19:35
  • Welcome to WPSE. Please update your post to ask an actual question. Also, when you mention "didn't work" please specifically share what the failure was. Pagination for custom loops has been covered in detail here: wordpress.stackexchange.com/a/120408/45202
    – jdm2112
    Dec 15, 2021 at 20:45
  • Does this answer your question? How to fix pagination for custom loops?
    – jdm2112
    Dec 15, 2021 at 20:45
  • Dexter, I've removed the image from your question. Please see kaiser's comment. Feel free to edit your question with an altered image. Thanks!
    – Christine Cooper
    Dec 15, 2021 at 21:41

1 Answer 1

0

Are you maybe looking for something like this?

/**
* Outputs blog / archive pagination
*
*/
function my_pagination(){
    global $wp_query;

    $total_pages = $wp_query->max_num_pages;

    if ($total_pages > 1):
        $current_page = max(1, get_query_var('paged'));
        echo paginate_links(array(
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text' => 'Prev',
            'next_text' => 'Next',
            'type' => 'list'
        ));
    endif;

}

Then include it in your theme likeso

<?php if ( $family_query->have_posts() ) : ?>
<?php while ( $family_query->have_posts() ) : ?>
    <?php $family_query->the_post(); ?>
    <h3><?php echo esc_attr( get_the_title() ); ?></h3>
<?php endwhile; ?>

<?php my_pagination(); ?>

Your Answer

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

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