0

I have the WordPress site below that displays a map of medical providers and a list below that of those same providers.

https://preview.scmamit.yourmark.com/?sfid=756&_sft_provider_type=primary-care

I need to show all of the results of a search on the map, but then the list should have pagination (only displaying 25 per page). I'm running through the loop twice, 1st to build the pins for the map, and 2nd to display the list.

If I turn off pagination to show all the pins on the map, how then would I display the list with pagination below the map?

Back in my Coldfusion days, I would have done a query of queries for the 2nd loop, and I've seen "pre_get_posts" and some other options that seem close to what I want, but nothing is quite getting me there.

2 Answers 2

0

Just run the loop a 2nd time:

First reset the 1st query at the end of it:

wp_reset_query();

And add this to your second query:

$count = get_option('posts_per_page', 10);
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$offset = ($paged - 1) * $count;

And add this args to the query:

'posts_per_page' => $count,
'paged' => $paged,
'offset' => $offset,

Regards Tom

0

Thought I would post what I did to get this work. Thanks to @tom-ukelove for getting me headed in the right direction.

I had the map, list and search filter on this page. The search filter is created with Search & Filter plugin.

https://scmamit.com/?sfid=756

In my Search & Filter settings, I set the results per page to 25, which is what I needed the list to do in loop #2.

To display all of the results on the map (loop #1), I used the following code to remove the 25 posts_per_page limit.

<?php
//get the search variables from URL
$this_keyword = $_GET['_sf_s'];
$this_provider_type = $_GET['_sft_provider_type'];
$this_city = $_GET['_sfm_city'];

//rebuild the query
$args = array(
'post_type' => 'providers',
'provider_type' => $this_provider_type,
's' => $this_keyword,
    'meta_query' => array(
        array(
            'key' => 'city',
            'value' => $this_city,
            'compare' => 'LIKE'
        )
    ),
//use -1 to remove the 25 results per page limit
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
);

$the_query = new WP_Query( $args ); 
global $wp_query;
// Put 25 limit query object in a temp variable
$tmp_query = $wp_query;
// Now purge the global query
$wp_query = null;
// Re-populate the global with the no-limit custom query
$wp_query = $the_query;
$count = $the_query->post_count;

//run the loop #1 and build map pins within.
while($the_query->have_posts()) : $the_query->the_post();

//echo out map pin code here, or whatever you need in the 1st loop.

endif;
endwhile;

//reset the query
wp_reset_query();

// Restore original 25 limit query object
$wp_query = null;
$wp_query = $tmp_query;
?>

<ul>
<?php
// run the 2nd loop with pagination
while(have_posts()) : the_post();

// some <ul> list here

endwhile;
?>
</ul>

<?php 
//display pagination
$numeric_posts_nav();
?>

Hope that helps someone in the future. Lmk if there's a more efficient way to do it.

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.