Tyler Hall
Tyler Hall

Reputation: 393

Using Relevanssi Query for Timber Pagination

I'm trying to find a way to pass the Relevanssi found number of posts into Timber pagination. Currently, it is using the default query's found number of posts instead of Relevanssi's.

My search.php template looks like this:

$searchQuery = get_search_query();
$args = array(
    'post_type'        => array('post', 'page'),
    'posts_per_page'   => 10,
    's'                => $searchQuery,
    'post_status'      => 'publish',
    'relevanssi'       => true,
    'paged'            => $paged
);
$query = new WP_Query();
$query->parse_query($args);
$relposts = relevanssi_do_query($query);
$postsLength = sizeof($relposts);

$context['found_count'] = $query->found_posts;
$context['found_posts'] = $relposts;

$context['pagination'] = Timber::get_pagination();

In case it matters, I then loop through the found_posts variable in the .twig template to populate the search results. Everything works as expected except pagination, which has more pages than necessary (Relevanssi found posts are fewer than the default search's found posts).

Any help is much appreciated, thanks!

Upvotes: 3

Views: 800

Answers (2)

Tyler Hall
Tyler Hall

Reputation: 393

Below is one way to fix this. I used a different method on another site but its been too long now for me to remember exactly how I did it.

$searchQuery = get_search_query();

$args = array(
    'post_type'        => array('post', 'page'),
    'posts_per_page'   => 10,
    's'                => $searchQuery,
    'post_status'      => 'publish',
    'paged'            => $paged
);
$query = new WP_Query();

// Pagination fix
$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $query;

$query->parse_query($args);

if ($searchQuery != '') {
    $context['title'] = "Search results for '" . $searchQuery . "'";
    $context['search_query'] = $searchQuery;
    $relposts = relevanssi_do_query($query);
    $postsLength = sizeof($relposts);
    $context['pagination'] = Timber::get_pagination();
}

$context['found_count'] = $query->found_posts;
$context['found_posts'] = $relposts;

// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;

You basically hijack WordPress' built-in search query by saving it in a temporary variable. Then you replace it with Relevanssi's query to get Timber's pagination and finally reset the query back to WordPress' (not sure if its necessary to set it back to the built-in query but there's also no downside AFAIK).

Upvotes: 0

benedict_w
benedict_w

Reputation: 3608

I managed to get a similar query working as follows:

$args = array(
    'post_type'        => array('post', 'page'),
    'posts_per_page'   => 10,
    's'                => $searchQuery,
    'post_status'      => 'publish',
    'relevanssi'       => true,
    'paged'            => $paged
);

$found_posts = new Timber\PostQuery($args);

$context['found_posts'] = $found_posts;
$context['pagination'] = $found_posts->pagination();

Upvotes: 3

Related Questions