0

I have an archive template which I am using to list all businesses ("listings"), and also have pagination in place, however there is also a Google map, with custom markers for all the businesses.

How can I achieve this using the pre_get_posts function, since one query requires pagination, and the other (map) I have just set to 999 posts_per_page? Or am I going about this the completely wrong way?

function listing_archive_custom_query( $query ) {
    if ( !is_admin() && $query->is_main_query() && is_post_type_archive('listing') ) {
        $query->set( 'posts_per_page', get_option('posts_per_page') );
        $query->set( 'post_type', 'listing' );
        $query->set( 'meta_key', 'featured' );
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'order', 'desc' );
        $query->set( 'facetwp', true );
    }
}
add_filter( 'pre_get_posts', 'listing_archive_custom_query' );
1

You need to have two queries, one handling the listings, the other handling the map.

Since you need pagination for the listings, I'd suggest you use pre_get_posts for that query, so that you can use Wordpress' default pagination out of the box.

For the map, create a new WP_Query in your archive template:

$args = array(
    'posts_per_page' => 999,
    'post_type' => 'listing',
);

$map = new WP_Query( $args );
|improve this answer|||||
  • That's what I actually have in place at the moment, though I was hoping there would be another way since using WP_query to create a new query doesn't play well with another plugin I have - FacetWP. +1'd for now, but will leave this open just a bit longer before I accept. – wickywills Nov 23 '17 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.