Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Mark

Custom Field Date Sorting not Working

Using WordPress 4.8.2 with Admin Columns Pro version 4.0.10.

We have a custom post type called “items” that has a custom field called “date” which is (as you guessed!) a date field. All the custom fields on the site are created manually in the theme (not using ACF). Our “Item” post type is using your Admin Columns plugin to give our users the ability to sort the posts on the backend by custom fields. We originally setup the site to be able to sort by the date inputed in the custom field, and it worked great on versions < 4.0 using this code:

/** Inline Edit for Admin Columns **/
function cf_set_custom_field_date_store_format( $value, $column, $id ){
  $column_type = 'column-meta';
  $custom_field_type = 'date';
  if ( $column_type == $column->get_type() && $custom_field_type == $column->get_field_type() ) {
    $value = date('Y-m-d', strtotime($value));
  }
  return $value;
}
add_filter( 'cac/inline-edit/ajax-column-save/value', 'cf_set_custom_field_date_store_format', 10, 3 );

Once we updated the plugin to version 4.0.3 (or 4.0.4… can’t remember exactly) back in June the column sorting stopped working for us. Our first attempt to fix this was to update the above code according to the 4.0 docs:

/** Inline Edit for Admin Columns **/
function cf_set_custom_field_date_store_format( $value, $column, $id ){
  $column_type = 'column-meta';
  $custom_field_type = 'date';
  if ( $column_type == $column->get_type() && $custom_field_type == $column->get_field_type() ) {
    $value = date('Y-m-d', strtotime($value));
  }
  return $value;
}

add_filter( 'acp/editing/save_value', 'cf_set_custom_field_date_store_format', 10, 3 );

This, however, didn’t fix the issue and we’re still stumped on what’s causing the problem. Any help or pointers as to how we can get the custom field date sorting to start working again for our custom post type?

3 years, 11 months ago
Stefan
Developer

Hi Mark,

I believe the snippet you used is not to fix the sorting issue.
By default we store the Ymd format for Custom Field dates and your snippet just changes this format to Y-m-d. But both formats should work with sorting. I’ve tested both cases on my local environment.

So I think the problem lies somewhere else. There might be some custom code in your theme or in another plugin that also alters the WP Query on the overview screens. Do you know if you have any other custom filters on your website that alters the WP Query? Maybe you can look for the pre_get_posts filter? You could install WP_Query to see the actual SQL query that runs for your overview. If you like I could also have a look on your environment. You can send login to support@admincolumns.com.

3 years, 11 months ago
Mark

Hey Stefan,

Thanks for the quick reply (and sorry for my late response!). You are correct, there is a custom pre_get_posts method being used in our custom theme’s functions.php file that is causing the error. Once I removed that from the code the issue went away and things worked as expected!

Again, I really appreciate the quick response!

3 years, 11 months ago
Stefan
Developer

You’re welcome and I’m glad I was able to help you.
I will close this ticket.

3 years, 11 months ago

You must be logged in to reply to this topic.