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.
$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 differentposts_per_page
values on the same query.$wp_query->found_posts
.is_main_query()
in the function, according to what you want. You can also have 2 pre_get_posts actions with different priority.