Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Paginate Search Result - Custom Post Type w/ Plugin WP Geo Mashup WordPress

  • REFUNDED

I am using the plugin WP Geo Mashup, and created a custom post type called Locations where I add the Lat & Long of each post location.

I am using the Geo Mashup Search Template to display my search results, which is working fine. However, I wanted to Paginate the search locations to only show 5 at a time.

When I use the following code to create the pagination, for the most part everything seems to work, however, when I go to page 2 of the pagination none of the posts show up.


<?php
//Protect against arbitrary paged values
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;

$args = array(
'posts_per_page' => 4,
'post_type' => 'location',

);

$geo_mashup_search = new WP_Query( $args );
?>




<?php
$big = 999999999; // need an unlikely integer

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

Answers (4)

2015-07-14

Hariprasad Vijayan answers:

Hi,

Have you tried like this

?php

//Protect against arbitrary paged values

$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
$args = array(
'posts_per_page' => 4,
'post_type' => 'location',
'paged' => $paged,
);
$geo_mashup_search = new WP_Query( $args );
?>


jpequens comments:

Yes, I've tried that and the same thing happens.

2015-07-15

Dylan Kuhn answers:

Hi! Unfortunately I think this will require features to be added to Geo Mashup. The closest thing you can do currently is limit the number of results using a filter, which would go in your theme's functions.php or a plugin file:



add_filter( 'geo_mashup_search_query_args', 'prefix_limit_results' );

function prefix_limit_results( $args ) {

$args['limit'] = 5;

return $args;

}


jpequens comments:

If I do that can you still paginate it or will that limit the pagination?


Dylan Kuhn comments:

That will just show the first 5 results - equivalent to showing only page 1. If a version of Geo Mashup is released with pagination capabilities down the road, you'd remove this code at that point to use it.

2015-07-15

Darlene Grace Arcenal answers:

Can you post the url here. It might be a permalink issue. Be sure to check if that page doesn't have a similar page or category names. The urls might be in conflict. Try to set the Permalinks settings to Default for now so we could check. If it works when you set your permalinks to default, that page indeed has a conflict. I had the same problem before, it turned out I have a page and a category with the same URL.


jpequens comments:

I also think it may be a permalinks issue. I changed to default permalinks and it still does not work. Here is a search form you can test:

http://floattanks.net/?page_id=142

It's not styled, so you will see the pagination between the map and the search results on the search results page. Do a search in the zip 10016 or nearby. I have 6 locations in that area and the pagination currently set to show 3.


jpequens comments:

I changed the permalinks back to post name to mess with it some more. You can now test on this page:

http://floattanks.net/search_form/


Darlene Grace Arcenal comments:

Hi,

Above $big = 999999999; line can you try adding global $wp_query;


Darlene Grace Arcenal comments:

disregard my previous asnwer. Try adding this to your functions.php

function my_post_count_queries( $query ) {
if (!is_admin() && $query->is_main_query()){
if(is_home()){
$query->set('posts_per_page', 1);
}
}
}
add_action( 'pre_get_posts', 'my_post_count_queries' );


jpequens comments:

Both did not work. Do I need to call that function in any particular way?


Darlene Grace Arcenal comments:

No, just add that on functions.php

2015-07-15

Bob answers:

please provide full code your custom post type and geo mashup search template.


jpequens comments:

Here is the full geo mashup search results code. Note - I've played with 'paged' and 'page' in the code. I had 'page' in my early code and have 'paged' in this code. This code was taken directly from the provided template from Geo Mashup



<?php
/**
* Default Geo Mashup Search results template.
*
*
*/


$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$args = array(

'posts_per_page' => 3,

'post_type' => 'location',

);

$geo_mashup_search = new WP_Query( $args );

?>

<div id="geo-mashup-search-results">

<h2><?php printf( __( 'Search results near "%s"', 'GeoMashup' ), esc_html( $search_text ) ); ?></h2>


<?php if ( $geo_mashup_search->have_posts() ) : ?>

<?php if ($object_name == 'post'):?>


<?php while ( $geo_mashup_search->have_posts() ) : $geo_mashup_search->the_post(); ?>

<div class="search-result" id="floatation-results-list">
<h3>
<a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a>
</h3>

<p id="float-location-price">
<?php the_field('price_dollars'); ?>
</p>

<p>
<a href="<?php the_permalink(); ?>" class="btn" id="geo-results-info-button">More Information</a>
</p>

<p id="search-results-address">
<?php the_field('address_1'); ?>
<br>
<?php the_field('city_location'); ?> <?php the_field('state_location'); ?>, <?php the_field('zip_location'); ?>
<p id="search-results-phone-number"><?php the_field('phone_number'); ?></p>
</p>

<p id="search-results-distance-links">
<?php _e( '', 'GeoMashup' ); ?>
<?php $geo_mashup_search->the_distance(); ?> | <a href="<?php the_permalink(); ?>/#location-details-map">Map</a> | <?php echo "Hours" ?>
</p>


</div><!-- end search-result #floation-results-list -->

<?php endwhile; ?>

<?php

$big = 999999999; // need an unlikely integer

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





<div id="floation-map-results">
<?php echo GeoMashup::map( array(
'name' => 'search-results-map',
'search_text' => $search_text,
'object_ids' => $geo_mashup_search->get_the_ID_list(),
'center_lat' => $near_location['lat'],
'center_lng' => $near_location['lng'],
'search_lat' => $near_location['lat'],
'search_lng' => $near_location['lng'],
'map_content' => 'global',
'object_name'=> $object_name,
'zoom' => $approximate_zoom + 1 // Adjust to taste
) ); ?>
</div>


<?php elseif ($object_name == 'user'):?>

<?php while ( $geo_mashup_search->have_posts() ) : $user=$geo_mashup_search->get_userdata(); ?>
<div class="search-result">
<h3><?php echo $user->first_name.' '.$user->last_name;?> aka <?php echo $user->user_nicename?></h3>
<p>
<?php _e( 'Distance', 'GeoMashup' ); ?>:
<?php $geo_mashup_search->the_distance(); ?>

</p>
</div>
<?php endwhile; ?>






<?php endif;?>
<?php else : ?>

<p><?php _e( 'No results found.', 'GeoMashup' ); ?></p>


<?php endif; ?>



</div>


Bob comments:

Are you sure a search result page is showing proper output?

whatever I add in search box it always show same output on search result.
I think it is not geo mashup search it's default wp query output of custom post type.

All other mashup related stuff is not useful here I think.

As per my opinion following mistakes are here.

1. Your arguments are missing searching related parameter. like <em>'s' => $_POST['location_text']</em>
2. You are missing <em>pre_get_posts</em> filter to search inside meta data of locations custom post type.
3. You are storing value in <em>$paged</em> but on using it in argument like <em>'paged' => $paged</em>

Now according to me the biggest reason when pagination is not working is explained below.
Go ahead and visit this page [[LINK href="http://floattanks.net/search_test/"]]http://floattanks.net/search_test/[[/LINK]] -- you will get nothing. Now visit http://floattanks.net/search_form/ and click search you will get result.

You get result because you received necessary value in $_POST there. which is handled by geo mashup plugin code.
for example see code <em>$object_name == 'post'</em> where this $object_name came from. it's created by geo mashup code.

Now when you click page 2 (I can see pagination text there "1 2 Next ยป") You are not submitting anything so there is nothing in $_POST, so the there will be no output. In short this 2nd page is same as visiting http://floattanks.net/search_test/ directly. No result at all. If you there is something submitted to paged=2 then there will be result too.

<strong>My Suggestion</strong>

1. Forget geo mashup search widget and create your own search related page templates
-> search form and search result.
- search form - you can copy current structure of search widget html to make it convenient.
- search result - here you need to generate proper arguments for wp_query and loop through it. you can also use query_posts if you wish.

Please note you have to manage search parameter on all page request. so instead of POST in method you can try GET method too.

*This is just idea you might face different issues during development.