Using pagenavi() with custom queries

Hello there,

We’re crafting our own custom search results page and we wanted to use pagenavi() for the pagination.

However it looks like it uses $wp_query and I’m not too sure how we can feed it our custom query.

I looked inside pagination.php but the options available doesn’t seem to let us do that.

Is that possible at all?

Thank you!

Hello @thisisbbc,

Thank you for the details.

There is a known issue in X 6.5.6 that messed up the search results. This issue only happens on version 6.5.6 of X theme. Could you please downgrade your X theme to the version 6.5.5?

I’ve attached the zip file of version 6.5.5 under Secure Note.

Here is a detailed guide for reinstalling the theme: Manual X Update Via FTP

Let us know how it goes!

Hello @RueNel,

Thanks for the quick reply.

Does this apply to Pro as well? Because we’re using Pro 2.5.5, not X

Waiting for your feedback!.

Hello @thisisbbc,

Yes this affects Pro 2.5.5 as well. If you have Pro 2.54, you may rollback to that version. I will follow up on this issue to our developers so that a fixed should be included in the next release update anytime soon.

Thank you for your understanding.

Can you tell me what exactly was messed up on the latest update?

I was able to fix my issue playing around with a few things. My first mistake was using post_count instead of found_posts. The first one return the number of results on the current page. The second one returns the total number of posts found.

Also, instead of resetting postdata before calling pagenavi() I did that after. I guess that makes sense but I’m still not super comfortable playing with the wp_query.

Anyway, it seems to work fine!

If anyone is looking to do the same…

Craft your custom query in search.php:

	global $query_string;
	wp_parse_str( $query_string, $search_query );

	$newsearch = new WP_Query( array(
		'post_type'			=> 'post',
		'post_status'		=> 'publish',
		'order_by'			=> 'post_date',
		'order'				=> 'ASC',
		'posts_per_page'	=> 6,
		'nopaging'			=> false,
		's'					=> $search_query[s]
	));

Then the main loop should look like this:

<div class="x-section" id="search-main">
	<div class="x-container max width">
		<?php if ( $newsearch->have_posts() ) : ?>
			<div id="x-iso-container" class="x-iso-container">
				<?php
					while ( $newsearch->have_posts() ) : $newsearch->the_post(); 
						$stack = x_get_stack();
						x_get_view( $stack, 'content', get_post_format() );
					endwhile;
				?>
			</div>
			<?php pagenavi(); ?>
			<?php wp_reset_postdata(); ?>
		<?php else : ?>
			<?php x_get_view( 'global', '_content-none' ); ?>
		<?php endif; ?>
	</div>
</div>

PS: We use the iso-container because we loaded the isotope script on our search results, but if you’re not loading the script you’ll have display issues.

Hello @thisisbbc,

A fix will be in the next release update. Meanwhile, to temporarily resolve your issue, please check out this thread:

Hope this helps.

Hello @RueNel,

I have applied the fix using our child theme functions.php.

However we still have some issues using pagenavi() on our search results page.

The page count returned is always the total number of pages for all results.

When users search for “Everything”, it works well. However our search results can be displayed for “Blog posts only” or “Products only” and then the pagination gets the wrong total number of pages. Any way to fix that?

Thank you for your help.

All best,
B

Hi @thisisbbc,

If you added the recommended code, then the whole override of the search functionality of the theme will be disabled. So the problem that now you are facing is not generated by our theme.

You can test that by changing the theme temporarily from Pro to another one and test the case, if the problem persists, then your problem is not related to our theme, and we are not the correct people to follow up on that, and it will be outside of our support scope.

Thank you for your understanding.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.