0

So I've been battling this for like a week now and it's impossible to find a solution, so I am reaching out to see if anyone could assist me.

  • I want to paginate through my total 13 posts and just display 4 at a time until I loop through all 13 (4 pages in total).
  • $acf_query has a total of 6 posts.
  • $wp_query has a total of 7 posts.

So here is the PHP code that I have:

    public function fetch_work_items()
    {
        $paged = (get_query_var( 'paged' )) ? get_query_var('paged'): 1;

        $args = [
            'post_status' => ['publish'],
            'post_type' => ['work', FH_Report_Type::CPT_NAME],
            'fields' => 'ids',
        ];

        $acf_query = [];
        if (get_field('featured_posts')) {
            $acf_post_ids = get_field('featured_posts', false, false);
            $acf_query = new WP_Query([
                'post_type' => ['work', FH_Report_Type::CPT_NAME],
                'post__in' => $acf_post_ids,
                'orderby' => 'post__in',
                'fields' => 'ids',
            ]);
        }

        $wp_query = new WP_Query($args);
        $main_ids = array_unique(array_merge($acf_query->posts, $wp_query->posts));
        $main_args = [
            'post_type' => ['work', FH_Report_Type::CPT_NAME],
            'post__in' => $main_ids,
            'orderby' => 'post__in',
            'posts_per_page' => 4,
            'paged' => $paged
        ];
        $main_query = new WP_Query($main_args);

        return $main_query;
    }

This gives me the following output:
I have a total of 13 posts, with 4 posts_per_page and it outputs a WP_Query object.

enter image description here

This is the response from $main_query->query.
enter image description here


Here is the pagination that I attempted:

<?php

$response = FH_Work::getInstance()->fetch_work_items();

?>
<?php if ($response->have_posts()) {
    while ($response->have_posts()) {
        $response->the_post();
        the_title();
    }
} else {
    echo 'No posts';
}

$big = 999999999;

echo paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $response->max_num_pages )
);
wp_reset_postdata();

Here is the the the_title() while loop and pagination response, but when I click on any number all it does it reddirect to the same page and nothing happens.

enter image description here

All help will be appreciated! I am completely losing it at this moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.