Two loops by pre_get_post on same page

I have two loops in archive.php, both are affected by pre_get_post.

My code on pre_get_post is this:

// My pre_get_post
function my_pre_get_posts( $query ) {
if ( is_admin() || ! $query->is_main_query() )
    return;

if ( is_post_type_archive( 'propiedad' ) ) {
    //Orderby

    $orderby = 'meta_value_num';
    $order="ASC";
    $paged = get_query_var('paged');

    //Tax query
    $tax_query = array();

    if( isset($_GET['operacion']) or isset($_GET['ambiente']) or isset($_GET['tipo']) ) {

        // Operacion
        if( isset($_GET['operacion']) && !empty($_GET['operacion']) ){

            $tax_query[] = array (
                'taxonomy' => 'operacion',
                'field' => 'slug',
                'terms' => $_GET['operacion'],
            );

        }

        // Ambiente
        if( isset($_GET['ambiente']) && !empty($_GET['ambiente']) ){

            $tax_query[] = array (
                'taxonomy' => 'ambiente',
                'field' => 'slug',
                'terms' => $_GET['ambiente'],
            );

        }

        // Tipo de propiedad
        if( isset($_GET['tipo']) && !empty($_GET['tipo']) ){
            $terms =  $_GET['tipo'];
            $tax_query[] = array (
                'taxonomy' => 'tipo_propiedad',
                'field' => 'slug',
                'terms' => $terms,
            );

        }

        //Tax relation
        $tax_query['relation'] = 'AND';

    }


    // Meta query
    $meta_query = array();

    if( isset($_GET['con_precio']) or isset($_GET['min_precio']) or isset($_GET['max_precio']) ) {

        // Con precio
        if( isset($_GET['con_precio']) && !empty($_GET['con_precio']) ){

            $meta_query[] =  array(
                'key' => 'operacion_mostrar-precio',               
                'value' => 1,          
            ); 

        }

        // Min y max precio
        if( isset($_GET['min_precio']) && !empty($_GET['min_precio']) && isset($_GET['max_precio']) && !empty($_GET['max_precio']) ){

            $meta_query[] =  array(
                'key' => 'operacion_precio-ar',               
                'value'   => array( $_GET['min_precio'], $_GET['max_precio'] ),
                'type'    => 'numeric',
                'compare' => 'BETWEEN',        
            ); 

        }                       

    }

    if( isset($_GET['order']) && $_GET['order'] == 'DESC') {
        $order="DESC";
    }

    //orderby
    $query->set( 'paged', $paged );
    $query->set( 'order', $order );
    $query->set( 'orderby', $orderby );
    $query->set( 'meta_key', 'operacion_precio-ar' );

    //seteo el tax query
    $query->set( 'tax_query', $tax_query );

    //seteo el meta query
    $query->set( 'meta_query', $meta_query );

    $query->set( 'posts_per_page', 4 );

    //echo '<pre>'; print_r($query); die();

    return;

    }
}
add_action( 'pre_get_posts', 'my_pre_get_posts', 1 );

As you see, there is a: $query->set( 'posts_per_page', 4 );

I need this in archive.php for the pagination, but I dont need that in the other loop, because I want to know how many results are filtered and get information by custom field of all the posts, but this show me only 4, I think is because the pagination affects it.

So I need to set, in the another loop, this: $query->set( 'posts_per_page', -1 ), but I don’t know how.

1 Answer
1

It will be good to know how you implement your code in header.php.

You can try to move some condition to not affect all query?

function my_pre_get_posts( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
      return;

    if ( is_post_type_archive( 'propiedad' ) ) {

Becomes

function my_pre_get_posts( $query ) {
  if(!is_admin()){
      if ( is_post_type_archive( 'propiedad' ) ) {
         // all your code here
         if($query->is_main_query()){
             $query->set( 'posts_per_page', 4 );
         }
         else{
             $query->set( 'posts_per_page', -1 );
         }
      }
  }

But the method is not the best, it will affect all query for this archive page (sidebar, menu). You need more conditions, when you implement your code in header.php you can define a do_action('header_pre_get_post'). and call the right posts_per_page in the pre_get_posts.

Hope it gives you some hint.

Leave a Comment