CPT and Query String sorting Problem

Hi,

i created a CPT “Short News”.
Here its possible to add a new Short News with ACF-Fields. One Field is “datum” (date).
The return value is “j. F Y”

I created a component to show all Short News with the current date or newer, depending on the field “datum”.

My Query String for the provider is:
post_type=short_news&posts_per_page=3&meta_key=datum&meta_value={{dc:global:date format=“j. F Y”}}&order=ASC&meta_compare=%3E%3D

But its not working.

How will i get it in the right order?
Thanks in advance.
Tobi

Hello Tobi,

Thanks for writing in! Please use this query instead:

$meta_query[] = array(
    'key'     => 'datum',
    'value'   => date('j. F Y'),
    'compare' => '>=',
 	'type' => 'DATE'
);

$query = array(
    'post_type'  => 'short_news',
    'meta_key'   => 'datum',
    'orderby'    => 'meta_value',
    'order'      => 'ASC',
    'meta_query' => $meta_query
);

Run the var_dump( http_build_query( $query ) ); in your child theme’s function.php file again so you can get the resulting query string.

Best Regards.

Oh thats really great. I hope now i know how to use it.
Thank you!

Hey @rok1975,

You’re welcome!

Hey,

sorry but i need further help, it was my fault.
It was my mistake it did not work the first time.
The order is working but the compare not.

The Query not working:
$meta_query[] = array(
‘key’ => ‘datum’,
‘value’ => date(‘j. F Y’),
‘compare’ => ‘>=’,
‘type’ => ‘DATE’
);

$query = array(
‘post_type’ => ‘short_news’,
‘meta_key’ => ‘datum’,
‘orderby’ => ‘meta_value’,
‘order’ => ‘ASC’,
‘meta_query’ => $meta_query
);

The Output:
post_type=short_news&meta_key=datum&orderby=meta_value&order=ASC&meta_query%5B0%5D%5Bpost_per_page%5D=3&meta_query%5B0%5D%5Bkey%5D=datum&meta_query%5B0%5D%5Bvalue%5D=6.+March+2023&meta_query%5B0%5D%5Bcompare%5D=%3E%3D&meta_query%5B0%5D%5Btype%5D=DATE

The Result:

Hi @rok1975,

The date function does not work here, you need to use the Dynamic Content to get the current date i.e. {{dc:global:date format=“j. F Y”}}. The Query String will look like the following.

$query = array(
    'post_type'           => 'post_type',
    'orderby'             => 'meta_value',
    'meta_key' 			  => 'datum',
    'meta_query' => array(
        array(
            'key'		=> 'datum',
            'compare'	=> '>=',
            'value'		=> {{dc:global:date format="d.m.Y"}},
            'type' 		=> 'DATE',
        )
    ),
    'order'               => 'asc'
);

Hope it helps.
Thanks

Hi @tristup,

okay now my Query String looks like
post_type=short_news&meta_key=datum&orderby=meta_value&order=ASC&meta_query%5B0%5D%5Bkey%5D=datum&meta_query%5B0%5D%5Bvalue%5D={{dc:global:date format=“j. F Y”}}&meta_query%5B0%5D%5Bcompare%5D=%3E%3D&meta_query%5B0%5D%5Btype%5D=DATE

Sorry. But it still not work.

Best,
Tobi

Hi Tobi,

If it is not working we need to check it through the WordPress admin dashboard. I would request you please provide login credentials for your site in a secure note to examine it further, including:

– WordPress Site URL & Login URL
– WordPress Admin username/password
– Specific page where you have added this Query String

To create a secure note, click the key icon underneath any of your posts.

Thanks

Hi Tobi,

I am sorry, I completely forgot to mention that the ACF store the date in Ymd format. The date format in the Query String should be {{dc:global:date format=“Ymd”}}. I have added that into the Query String and now it is working as expected.

Thanks

@tristup

Great! Thank you for the fast support.

Best,
Tobi

Of course, we’ll always be here for you.
Please don’t hesitate to create another thread if you need any further assistance.

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