Skip Navigation

[Closed] Need secondary sorting based on custom field

This support ticket is created 3 years, 8 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 29 replies, has 2 voices.

Last updated by Shane 3 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1731279

Tell us what you are trying to do? Need to sort search results first on title ASC then based on the child's custom field (wpcf-year) DESC. Tried customizing code from here: https://toolset.com/forums/topic/custom-order-by-with-2-custom-fields/#post-387775

add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fiels_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != 115 ){
return;
}

$meta_query = array();
$meta_query[] = array('key' => 'wpcf-title');
$meta_query[] = array('key' => 'wpcf-year');
if ( !isset($query_args['meta_query']) ){
$query_args['meta_query'] = array();
}
$query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
add_filter('posts_orderby', 'custom_order');

return $query_args;
}
function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_date ASC';
}

I'm not sure what the 99, 3 is for and this doesn't work - give error on page:

"Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'add_custom_fields_to_view_query' not found or invalid function name in /var/www/wordpress/hqinstruction/wp-includes/class-wp-hook.php on line 287"

What is the link to your site? hidden link

#1731321

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Thank you for getting in touch.

It should be noted that even through this is not something that is supported by our Toolset team as sorting by multiple custom fields is not supported.

However based on your error I see where you are writing the function name incorrectly. It should be add_custom_fields_to_view_query and not add_custom_fiels_to_view_query

Please let me know if this helps.
Thanks,
Shane

#1731365

Thank you, that got rid of the error but it is not showing any results now. I think I'm requesting the title incorrectly?

I tried both wpcf-post-title and wpcf-title but neither works.

Do I need to change this part at the end as well? return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_date ASC';

I want to sort by the post title first, then the Child's year.

// ADD CUSTOM FIELDS
add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != 115 ){
return;
}

$meta_query = array();
$meta_query[] = array('key' => 'wpcf-post-title');
$meta_query[] = array('key' => 'wpcf-year');
if ( !isset($query_args['meta_query']) ){
$query_args['meta_query'] = array();
}
$query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
add_filter('posts_orderby', 'custom_order');

return $query_args;
}
function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_date ASC';
}

#1731413

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Is the title the default post title that comes with wordpress?

Please let me know as you will need to modify the query itself in order to do this.

Thanks,
Shane

#1731415

Yes, it is the default post title.

#1733189

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dung,

Try changing this function below.

function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_title ASC';
}

Additionally you know that our views allows for this out the box. You are able to sort by a custom field and a secondary sorting for the post title, you don't need to write a custom code for this. I was under the impression that both fields were custom fields. Given that they aren't you can do this through the sorting option for our view.

Thanks,
Shane

#1733537

Hi Shane,

Thanks but what I want to do is 1st to order by standard post_title field ASC then 2nd order by the custom "year" field DESC.

This doesn't appear to be an option in the interface and the code you suggest does not change how it is currently working.

#1734687

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dung,

Unfortunately I wouldn't be able to modify the code to work for this scenario as the custom coding is out of our support scope. This is because this code is written to work with 2 custom fields so it will need some modifications or rather a new custom code in order to get this sorting to work.

I did check in the forms to see how we can get get some reference code and this may help.

add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != 115 ){
return;
}
$query_args = array( 
  'meta_query'      => array(
    'year'   => array(
      'key'     => 'wpcf-year',
      'compare' => 'EXISTS',
    )
  ),
  'orderby'     => array(
      'title' => 'ASC',
      'year' => 'ASC')
  )
);

return $query_args;
}

Please let me know if this code itself works. If it doesn't then I would recommend getting in touch with a developer in order to get further assistance with this.

Thanks,
Shane

#1734789

This code results in "no results" (and has and extra ) in it). The code only needs to address 1 custom field.

The goal is to sort like this:
1. by STANDARD WP post title field ASC
2. by CUSTOM TOOLSET year field DESC

#1734867

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dung,

Try,


add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != 115 ){
return;
}
$query_args = array( 
'post_type' =>'my post type slug',
    'posts_per_page' => -1,
  'meta_query'      => array(
    'year'   => array(
      'key'     => 'wpcf-year',
      'compare' => 'EXISTS',
    )
  ),
  'orderby'     => array(
      'title' => 'ASC',
      'year' => 'DESC')
  )
);
 
return $query_args;
}

Replace "my post type slug" with the actual slug of your post type that the view is displaying.

Thanks,
Shane

#1734921

I think we are getting closer. This code returns results but the secondary sorting by year is not working.

Secondary sorting seems to be sorting defaulting to post date?

CODE:

add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != 115 ){
return;
}
$query_args = array(
'post_type' =>'curricula',
'posts_per_page' => -1,
'meta_query' => array(
'year' => array(
'key' => 'wpcf-year',
'compare' => 'EXISTS',
)
),
'orderby' => array(
'title' => 'ASC',
'year' => 'DESC',
)
);

return $query_args;
}

#1734947

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi dung,

I believe I see the issue.

add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != 115 ){
return;
}
$query_args = array(
'post_type' =>'curricula',
'posts_per_page' => -1,
'meta_query' => array(
'cust-year' => array(
'key' => 'wpcf-year',
'compare' => 'EXISTS',
)
),
'orderby' => array(
'title' => 'ASC',
'cust-year' => 'DESC',
)
);

return $query_args;
}

Try this now.

#1735027

This does not work - result show but aren't sorting by the year desc.

Would you like admin access to the site?

Meanwhile the client has decided they want secondary ordering by the custom grades-radio field not the year field. That shouldn't change this request much except that I'll need to order the results by the "value to show" rather than "custom field content" because they need to go in the order K-2, 3-5, 6-8.

Modified code which also does not work:

add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != 115 ){
return;
}
$query_args = array(
'post_type' =>'curricula',
'posts_per_page' => -1,
'meta_query' => array(
'cust-grades' => array(
'key' => 'wpcf-grades-radio',
'compare' => 'EXISTS',
)
),
'orderby' => array(
'title' => 'ASC',
'cust-grades '=> 'ASC',
)
);

return $query_args;
}

#1735033

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dung,

As mentioned previously this would be something out of the scope of our support forum and given that we weren't able to resolve it with a few changes, this would mean some additional steps are needed. Whenever I checked out forums customers are usually asking for sorting with 2 custom fields.

As recommended before it would be best to get a developer involved to modify the code in order for it to display the correct sorting. The most I can do is to look for syntactical errors.

Thanks,
Shane

#1735335

I did some more digging about and found this post https://toolset.com/forums/topic/sorting-a-view/#post-397558

And this code is almost working. The issue is I can't seem to move post_title to be the first thing to sort because then it shows an error on the page. This does not work: return $wpdb->postmeta.'.post_title ASC, meta_value DESC';

Could you help with making that change?

add_filter('wpv_filter_query', 'add_custom_fields_to_curricula_view_query', 99, 3);

function add_custom_fields_to_curricula_view_query($query_args, $view_settings, $view_id ) {

if ($view_id == 115 ) {

$meta_query = array();
$meta_query[] = array('key' => 'wpcf-grades-radio');

if ( !isset($query_args['meta_query']) ){
$query_args['meta_query'] = array();
}

$query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
add_filter('posts_orderby', 'custom_order_curricula');

return $query_args;
}
}

function custom_order_curricula($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value ASC, post_title ASC';
}

changing the order shows this error:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DESC, post_modified DESC' at line 3]
SELECT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND wp_posts.ID NOT IN (19) AND ( wp_postmeta.meta_key = 'wpcf-grades-radio' ) AND wp_posts.post_type = 'curricula' AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')) GROUP BY wp_posts.ID ORDER BY wp_postmetapost_title ASC, .meta_value DESC, post_modified DESC

The topic ‘[Closed] Need secondary sorting based on custom field’ is closed to new replies.