2

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.

9
  • Do both loops operate on the same query object ($wp_query)? It sounds like you want to query for everything, then manually extract the current page's posts in the second loop. You can't have two different posts_per_page values on the same query.
    – Milo
    Commented Nov 17, 2016 at 4:36
  • 'to know how many results are filtered' - just the number of posts, or anything else? the number should be obtainable with $wp_query->found_posts .
    – Michael
    Commented Nov 17, 2016 at 5:12
  • You can add a statement with is_main_query() in the function, according to what you want. You can also have 2 pre_get_posts actions with different priority.
    – Benoti
    Commented Nov 17, 2016 at 9:13
  • @Michael And I need it to get some custom fields where they have the price of the different properties. Commented Nov 17, 2016 at 17:30
  • @Benoti Yes, I know how to do that, but I don't know how I specify when the loop is on the header.php. Commented Nov 17, 2016 at 17:33

1 Answer 1

0

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.

3
  • if ( is_post_type_archive( 'propiedad' ) ) { if(have_posts()) : while (have_posts()) : the_post(); endwhile; endif; } I implement that code in header.php and archive.php, the loop only work when 'header.php' is called in archive.php whit include 'header.php' and its works fine, but still not have differences between the two loops. Commented Nov 17, 2016 at 19:00
  • Why do you want to add the same code ? What are you trying to print in header.php ? Best option will be to make a new query for the header and use the pre get posts for the archive listing.
    – Benoti
    Commented Nov 17, 2016 at 20:43
  • I need the same filter, in one loop, I need to get information of the prices about the differents properties filtered, but it show me only 4, that's why I want to change $query->set( 'posts_per_page', 4 ) to $query->set( 'posts_per_page', -1) in the header.php. The reason about what I want to do that is because that show me only the prices of the current page (4 properties), and for example now there are 10 properties filtered, but I think the posts_per_page is the problem that the filter show me the information of the current page. Commented Nov 17, 2016 at 21:05

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.