Adding another ORDERBY for the shortcode
-
Hello,
I’m using your plugin together with the theme Listify.
I need to sort the listing by “RATING” – which is a integer hold in $number.
How do I go about adding it as orderby, so I can sort a listing by rating and do it easy via the short code like:
[jobs per_page=”10″ orderby=”RATING” show_filters=”false” show_categories=”false” categories=”16″ show_pagination=”false”]
Let me know, thanks.
-
the rating – is already a customer field. So somehow it would be to sort by that custom field – but I have had time figure out how to add this in e.g functions.php
Would I be able to do something similiar to:
$args = array( 'orderby' => 'meta_value', 'meta_key' => 'rating', 'meta_query' => array( array( 'key' => 'rating', 'value' => null, 'compare' => '!=' ) ), // End of meta_query 'fields' => 'ID', 'exclude' => array( 1 ), );You’d need to do something like your last post, but hooked into here with a custom function to set the args:
https://github.com/mikejolley/WP-Job-Manager/blob/master/wp-job-manager-functions.php#L113
meta_value_num would also be more suitable.
So just like this ?
$query_args = apply_filters( 'get_job_listings_query_args', $query_args, $args ); $args = array( 'orderby' => 'meta_value', 'meta_key' => 'rating', 'meta_query' => array( array( 'key' => 'rating', 'value' => null, 'compare' => '!=' ) ), // End of meta_query 'fields' => 'ID', 'exclude' => array( 1 ), );No, thats not how filters are used. https://pippinsplugins.com/a-quick-introduction-to-using-filters/
Thanks for the info.
I have tried to follow it, can you let me know if I’m getting closer :
$query_args = apply_filters( 'get_job_listings_query_args', $query_args, $args ); function addRating($query_args){ $args2 = array( 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'rating', 'meta_query' => array( array( 'key' => 'rating', 'value' => null, 'compare' => '!=' ) ), // End of meta_query 'fields' => 'ID', 'exclude' => array( 1 ), ); $query_args = array_merge('get_job_listings_query_args', 'addRating'); return $query_args; } add_filter( $query_args,'addRating');Afraid not π Here is a basic function you can start with:
add_filter( 'get_job_listings_query_args', 'custom_get_job_listings_query_args' ); function custom_get_job_listings_query_args( $args ) { // Do something to $args here, such as set $args['orderby'] = 'x'; return $args; }Really appreciate your help,
Made as simple as possible
$query_args = apply_filters( 'get_job_listings_query_args', $query_args, $args ); add_filter( 'get_job_listings_query_args', 'custom_get_job_listings_query_args' ); function custom_get_job_listings_query_args( $args ) { $args['orderby'] = 'rating'; return $args; }But here I get “Cannot redeclare custom_get_job_listings_query_args() (previously declared”
What is the mistake on this?
The first line in your paste is not needed.
And you must have used custom_get_job_listings_query_args() before π look for a same-named function in your code.
I still get the same error after edit the function name
add_filter('get_job_listings_query_args', 'args_function_rating_dsaw'); function args_function_rating_dsaw( $args ) { $args['orderby'] = 'rating'; return $args; }The issue is that the error is stated for the line of the function name.
So to be clear it states it’s alerady declared in the line right under add_filter
it’s somehow because it’s implemented within function get_job_listings( and each time this function is called it gets redeclared or that’s at least my assumption.
Got it solved by moving the function outside and declaring it to:
function args_function_rating_dsaw($args) { $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; $args['meta_key'] = 'rating'; return $args; }Many thanks for the help!
You need to put your code inside functions.php of your theme. I hope that is what you’re doing?
Yes of course π
But the new function was inside the function get_job_listings_query_args
Which made the error of already declared so each time get_job_listings_query_args was run it was redeclaring the new function args_function_rating_dsaw – so I moved it outside of get_job_listings_query_args but still in functions.php of course:)
Works perfectly
The topic ‘Adding another ORDERBY for the shortcode’ is closed to new replies.