Skip Navigation

[Resolved] wpv_filter_query custom ordering being overwritten

This support ticket is created 7 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 7 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#457455

I have installed a plugin that creates a post_views table in WordPress: hidden link

It lets me order queries by "post_views". I have tried to set up a filter so I can order my toolset views like this:

add_filter( 'wpv_filter_query', 'most_viewed_video_args' );

function most_viewed_video_args( $query_args ) {

	$query_args['orderby'] = 'post_views';
	$query_args['order'] = 'DESC';

	return $query_args;
}

This doesn't work. In fact it doesn't matter whatever I select in orderby, "rand", "ID" or whatever it never makes any difference.

This is the debug output for wpv_filter_query

wpv_filter_query
Array
(
    [post_type] => Array
        (
            [0] => eastwood-video
        )

    [paged] => 1
    [suppress_filters] => 
    [ignore_sticky_posts] => 1
    [posts_per_page] => 4
    [post__not_in] => Array
        (
            [0] => 10
        )

    [wpv_original_limit] => 4
    [wpv_original_offset] => 0
    [wpv_original_posts_per_page] => -1
    [post_status] => Array
        (
            [0] => publish
            [1] => private
        )

    [orderby] => views
    [order] => DESC
)

Why does is say "views" instead of "post_views"?

Also in wpv_filter_query_post_process debug info says views.

What can i do to change the order of the query? In every view there is a drop down for selecting the order and this seems to be the only thing that lets me change order.

#457552

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Mike

I'm guessing from your question that the post-views-counter adds post meta with a key post_views to the posts to track the views, and that is what you want to search by.

Firstly, you'll need to read up the correct way to use post meta data with orderby. The WordPress codex describes it here: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters, where you will see that you need to add a meta_key value as well as the orderby parameter. If the codex itself is not clear it is easy to find examples online.

Then when you use wpv_filter_query you need to specify a priority, and it is recommended that you add a high priority of, say, 101, so that your function runs after any others. If you leave it with the default priority of 10 other hooks may run after yours.

Note that you are also not running any checks to see what view you are on, so this will affect all views (if you have more than one on your site).

You probably want to set your code up something like this:

add_filter( 'wpv_filter_query', 'most_viewed_video_args', 101, 3 );
function most_viewed_video_args( $query_args, $view_settings, $view_id ) {
	
	if ( $view_id == 27 ) { // edit the id

		// your code here

	}

	return $query_args;
}
#457788

No. Forget the fact I mentioned a plugin entirely to simplify this.

I am unable to effect order by using the wpv_filter_query filter. Toolset always makes the query based on the order selected on the view template. Lets say I set it to "Rand" via filter but in the view its set to "date" then it will make the query ordered by date. How can I set this via the filter without it being overridden?

#458026

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Mike

It's the priority. I ran some tests and the magic number is 101.

This does not override the View settings:

function custom_ordering( $view_args, $view_settings, $view_id ) {

	$view_args['orderby'] = 'title';
	$view_args['order'] = 'ASC';

	return $view_args;
}
add_filter( 'wpv_filter_query', 'custom_ordering', 100, 3 );

This does:

function custom_ordering( $view_args, $view_settings, $view_id ) {

	$view_args['orderby'] = 'title';
	$view_args['order'] = 'ASC';

	return $view_args;
}
add_filter( 'wpv_filter_query', 'custom_ordering', 101, 3 );
This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.