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

Merge two $query->set queries WordPress

  • REFUNDED

Hi!
I'm having this site where the user, via a drop down menu, can choose how the posts should be sorted (asc, desc, by views etc...)

The url to the page becomes this:

http://mywebsite.com/?sort=views

Then, in functions.php I'm having this function:

$sort = get_query_var( 'sort' );

/** change the order of posts in the main query */

if ( $query->is_main_query() && $sort ) {

// change 'order' (ASC or DESC)

if ( in_array( $sort, array( 'ASC', 'DESC' ) ) ) {

$query->set( 'order', $sort );

// most views

} else if ( 'views' == $sort ) {

$query->set( 'v_sortby', 'views');
$query->set( 'v_orderby', 'desc' );
$query->set( 'v_timespan', 'month' );
add_filter('posts_fields', 'post_views_fields');
add_filter('posts_join', 'post_views_join');
add_filter('posts_where', 'post_views_where');
add_filter('posts_orderby', 'post_views_orderby');


} else if ( 'price' == $sort ) {

$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', $sort );
$query->set( 'order', 'ASC' );
}
}
}


My problem has to do with the views part of the code, so keep your focus there!

To keep track of the views, I'm using the Post Views plugin:

http://wordpress.org/extend/plugins/post-views/

As you can see in the code above, I'm filtering the popular posts by views to monthly, to keep the results as relevant and fresh as possible.

Now to my problem:

Some posts haven't got any views the latest month, but I would still like them to show up. So, I'm wondering if it's possible to combine two queries? First fetch the most popular posts this month, and after this fetch and display the rest of the posts like a normal wordpress query, but exclude the popular posts already displayed.

Hope I'm making myself clear, just ask me if you need more information!

Thanks!

// Jens.

Answers (1)

2013-05-08

Arnav Joy answers:

can you share full code please


Jens Filipsson comments:

Sorry, missed a part:

add_action( 'pre_get_posts', 'custom_pre_get_posts' );

function custom_pre_get_posts( $query ) {

// don't affect wp-admin screens

if ( is_admin() )

return;

// vars

$sort = get_query_var( 'sort' );

/** change the order of posts in the main query */

if ( $query->is_main_query() && $sort ) {

// change 'order' (ASC or DESC)

if ( in_array( $sort, array( 'ASC', 'DESC' ) ) ) {

$query->set( 'order', $sort );

// most views

} else if ( 'views' == $sort ) {

$query->set( 'v_sortby', 'views');
$query->set( 'v_orderby', 'desc' );
$query->set( 'v_timespan', 'today' );
add_filter('posts_fields', 'post_views_fields');
add_filter('posts_join', 'post_views_join');
add_filter('posts_where', 'post_views_where');
add_filter('posts_orderby', 'post_views_orderby');


} else if ( 'pris' == $sort ) {

$query->set( 'orderby', 'meta_value_num' );

$query->set( 'meta_key', $sort );

$query->set( 'order', 'ASC' );

}

}

}


Jens Filipsson comments:

And here is the form for the drop-down:

<form action="" method="GET">

<select name="sort" id="sort">

<option value="senaste" <?php selected( get_query_var( 'sort' ), 'DESC' ); ?>>Visa senaste först</option>

<option value="views" <?php selected( get_query_var( 'sort' ), 'views' ); ?>>Visa populärast först</option>

<option value="pris" <?php selected( get_query_var( 'sort' ), 'pris' ); ?>>Visa billigast först</option>

</select>

<button type="submit" id="sort-submit">Sortera</button>

</form>


Arnav Joy comments:

try this

add_action( 'pre_get_posts', 'custom_pre_get_posts' );



function custom_pre_get_posts( $query ) {



// don't affect wp-admin screens



if ( is_admin() )



return;



// vars



$sort = get_query_var( 'sort' );



/** change the order of posts in the main query */



if ( $query->is_main_query() && $sort ) {



// change 'order' (ASC or DESC)



if ( in_array( $sort, array( 'ASC', 'DESC' ) ) ) {



$query->set( 'order', $sort );



// most views



} else if ( 'views' == $sort ) {



$query->set( 'v_sortby', 'views');

$query->set( 'v_orderby', 'desc' );

add_filter('posts_fields', 'post_views_fields');

add_filter('posts_join', 'post_views_join');

add_filter('posts_where', 'post_views_where');

add_filter('posts_orderby', 'post_views_orderby');





} else if ( 'pris' == $sort ) {



$query->set( 'orderby', 'meta_value_num' );



$query->set( 'meta_key', $sort );



$query->set( 'order', 'ASC' );



}



}



}


Jens Filipsson comments:

Hello again!
No, the timespan paramenter needs to be there, although it should say <strong>month</strong>, not today. This is a working query which does what I want, but I need to merge it with a standard wordpress query, to make the posts without views show up as well.