FacetWP paging custom wp_query

Question

firstly the overview of what I’m trying to achieve. I’m building a resource center which lists out various custom post types and it’s filterable/searchable thanks to FacetWP. Within this listing of assets I am inserting a "promo" post type at every 6th spot, hence the custom query, which is:

add_filter('facetwp_query_args', function ($query_args, $class) {

if ('resource_library_parent' == $class->ajax_params['template']) {
    // promos query
    $promos = get_posts(array(
        'posts_per_page' => -1,
        'post_type'      => 'promo',
        'facetwp'        => false,
        'orderby'        => 'menu_order'
    ));

    // assets query
    $assets = get_posts(array(
        'posts_per_page' => -1,
        'post_type'      => array('report', 'ebook', 'whitepaper', 'casestudy', 'webinar', 'tool', 'infographic'),
        'post_status'    => 'publish',
        'has_password'   => false,
        'facetwp'        => false,
        'orderby'        => 'date',
        'tax_query'      => array(array(
            'taxonomy'   => 'tag',
            'field'      => 'slug',
            'terms'      => 'hide-me',
            'operator'   => 'NOT IN'
        ))
    ));

    $x = 6;

    foreach ($assets as $num => $asset) {
        if (($num + 1) % $x == 0) {
            if (isset($promos[($num + 1) / $x - 1])) {
                array_splice($assets, $num, 0, array($promos[($num + 1) / $x - 1]));
            }
        }
    }

    foreach ($assets as $asset) {
        $posts_id[] = $asset->ID;
    }

    $query_args['post_type'] = array('report', 'ebook', 'whitepaper', 'casestudy', 'webinar', 'tool', 'infographic', 'promo');
    $query_args['post__in'] = $posts_id;
    $query_args['orderby'] = 'post__in';
    $query_args['posts_per_page'] = 12;
}
return $query_args;
}, 10, 2);

This works beautifully on page load.

But clicking through the pagination (using the pager facet type) results in the second page of results missing the next "promo" at position 18. Refreshing the second page results in the promo showing at the correct spot – position 6 or 18 of the total. Clicking back to page one results in all 3 "promo" posts being displayed, again refreshing fixes the ordering.

It’s driving me around the bend and no doubt it has something to do with the pager or me altering the query in the wrong place, but ANY hints or ideas would be most welcome.

You can see a demo of this in action here: https://staging-xucobuce.kinsta.cloud/resource-center

0
rjdvsk 2 months 0 Answers 10 views 0

Leave an answer