Essential Grid order problems (sort by custom-meta tag)

Hi there,

I followed the instructions on this post to sort my grid by date and hide the ones that are in the past:

But it doesn’t seem to work the way it should. Currently I have four posts with custom-meta tags:

2019-11-29
2020-02-06
2020-02-07
2020-02-21

But they get sorted like this:

2019-11-29
2020-02-07
2020-02-21
2020-02-06

It would be great if you could have a look into it.

Best Regards, Christian

Hello Christian,

Thanks for writing in! You seem to forget to send us the name of the grid. You have 25 grids all in all and we do not have any clue which of the grid you are trying to add the “Sort by Custom Meta”.

Please provide us the name of the grid so that we can check it.

Cheers.

Oh, sorry. It’s the one with the id 25 (Verlosungen).
I also addressed this one in the function.php file. Could it be a problem with the date format because the website is in German?

Hello @Indiespect,

I have checked your setup. The meta key you are sorting on code is this eg-konzertdatum-sortierung. But then based on Essential Grid skin editor, the one you are displaying is eg-konzertdatum. You do have 3 custom meta on the skin editor. None of it was eg-konzertdatum-sortierung. Please try to display eg-konzertdatum-sortierung and check again.

Hi Lely

Thanks for your feedback. I would like to use the meta eg-konzertdatum-sortierung only for sorting and not to display on the grid itself. Isn’t that possible? In my posts I filled out that meta key with the dates in the format Y-m-d.

I tried to insert it to the skin with 0% opacity but it still doesn’t sort the grid elements in the right order.

Best, Christian

Hi Christian,

You can try deleting that layer from the skin if you don’t intend to display it. Using different meta for filtering and displaying has no effect on Wordpress query at all, the effect is if you use the wrong meta that doesn’t exist from the item itself (the item and not the skin).

Thanks!

Sorry Rad, but I integrated it after Lelys answer. Before that it wasn’t on the skin itself. So there is no change if I delete it again. It still doesn’t work in the right order.

In my previous post I asked if it might have something to do with the date format. The page itself has the German format (e.g. 12. November 2019) and the Essential Grid Y-m-d (e.g. 2019-11-12)

Best, Christian

Hey Christian,

The issue is with your Custom Meta. Its Sort Type is Numeric.

But, the data is a string because of the dashes between numbers.

If you remove the dashes, it will return to a number form and your posts will be sorted.

Hope that helps.

Hi Christian,

that’s perfect thanks.

One last question. I assume that I have to hide the posts manually as soon as the date is past, is that correct?
Did you check out my original post as well? I referred to an article from your support forum with a manual on how to achieve that automatically.

There it said the following:

Also, make sure to enter the date in YYYY-MM-DD formate in your custom field, for example: 2018-04-10 for today.

Best, Christian

Hi Christian,

In that case, what you need to implement should be similar to the last code here https://www.themepunch.com/faq/advanced-additional-parameters/

The comparison should be of timestamp instead of the actual date. Which of course, your custom meta should be a value of timestamp as well instead of date. You can use this tool https://www.timestampconvert.com/ and copy the time in seconds as your custom meta date value.

This is the references of the current_time() used in the code https://developer.wordpress.org/reference/functions/current_time/

if ( 'timestamp' === $type || 'U' === $type ) {
        return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
    }

The comparison is numeric since time in seconds are pure numbers. Filtering of date will work as date format such as YYYY-MM-DD, that is if you’re just filtering it with that exact date. But what you wish to do is to filter the future and past dates so the comparison is lower or greater which will only work on timestamps instead of date.

Thanks!

Hi Rad,

that sounds like the perfect solution for me. I only have a problem with that code that I have to place inside of the functions.php.

I created a new Custom Meta for the timestamp to sort the events. That worked well. But when I copy the code into my functions.php (with the grid number changed to mine) it doesn’t show any event. I’ve integrated it like that:

add_filter('essgrid_query_caching', 'eg_stop_caching', 10, 2);
add_filter('essgrid_get_posts', 'eg_mod_query', 10, 2);
 
// turn off caching for your grid
function eg_stop_caching($do_cache, $grid_id) {
 
if($grid_id == 25) return false;
return true;
 
}
 
function eg_mod_query($query, $grid_id){
 
// show only upcoming events in the future
if($grid_id == 25){
 
$query['meta_query'] = array( 'key' => '_start_ts', 'value' => current_time('timestamp'),
'compare' => '>=', 'type'=>'numeric' );
$query['meta_key'] = '_start_ts';
$query['meta_value'] = current_time('timestamp');
$query['meta_value_num'] = current_time('timestamp');
$query['meta_compare'] = '>=';
 
}
 
return $query;
 
}

I removed it for the moment so that the events are there again. May you see my mistake here?

I really appreciate your efforts and your patience.

Best, Christian

Hi Christian,

The custom meta key used from the code is _start_ts but I can’t find it from your post’s meta. In that case, please change the _start_ts from the above codes with your correct and preferred custom meta name. Like eg-timestamp, and this sample

$query['meta_query'] = array( 'key' => 'eg-timestamp', 'value' => current_time('timestamp'),
'compare' => '>=', 'type'=>'numeric' );
$query['meta_key'] = 'eg-timestamp';

Hope this helps :slight_smile:

Hi Rad,

You are my hero! Thanks a lot to all for your help, it seems to work now :slight_smile:

Have a great day.

You’re welcome!
We’re glad @Rad were able to help you out.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.