Query String for Looper with global date shortcode

I have a working query string to output my custom post type (Classifieds) posts with a start-date before today and end-date in the future. Unfortunately, the string output includes today’s date at the time of producing the query string, not the current day’s date.

  1. Is there any way I can get it to include the global date shortcode? The whole process is tiring - especially if you are producing several of them.
  2. Assuming I can insert the global date shortcode somehow, could I use code snippets to produce a shortcode with the exact query string text - and use this within the Looper’s query string? I tried using the shortcode [code_snippet id=9 php=true] in the query string field but it did not work.
  3. Another solution would be to use the array key option and code snippets - however, I have no idea how to generate the array key. Using an array key (or shortcode) would mean updating several loopers would be very easy.

Any help would be appreciated

The website I’m building is: https://woodworks.news/classifieds-listings/
Here is my current array to produce the string:

<!DOCTYPE html>
<html>
<body>

<?php

$query = array(
'post_type'      => 'classifieds', // my custom post type    
'post_status'      => 'publish',
'posts_per_page' => '500',

'meta_query' => array(
    'relation' => 'AND',
    array(
        'key'     => 'classifieds_start_date',
        'value'   => date("Y-m-d"),
        'compare' => '<=',

    ),
    array(
        'key'     => 'classifieds_end_date',
        'value'   => date("Y-m-d"),
        'compare' => '>=',

    )
),
'orderby' => 'random',
);     
echo http_build_query( $query );

?>

</body>
</html>

and here is the output when I use the Code Snippets code - ‘[code_snippet id=9 php=true]’:

post_type=classifieds&post_status=publish&posts_per_page=500&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=classifieds_start_date&meta_query%5B0%5D%5Bvalue%5D=2022-10-05&meta_query%5B0%5D%5Bcompare%5D=%3C%3D&meta_query%5B0%5D%5Btype%5D=DATE&meta_query%5B1%5D%5Bkey%5D=classifieds_end_date&meta_query%5B1%5D%5Bvalue%5D=2022-10-05&meta_query%5B1%5D%5Bcompare%5D=%3E%3D&meta_query%5B1%5D%5Btype%5D=DATE&orderby=random

In follow-up, I finally got the query string working with the global date shortcode. However, it is still a long process and a more automated solution would be better - ie via array key with Code Snippets.
Here is the working query string with global date shortcode:

post_type=classifieds&post_status=publish&posts_per_page=500&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=classifieds_start_date&meta_query%5B0%5D%5Bvalue%5D={{dc:global:date format=“Y-m-d”}}&meta_query%5B0%5D%5Bcompare%5D=%3C%3D&meta_query%5B0%5D%5Btype%5D=DATE&meta_query%5B1%5D%5Bkey%5D=classifieds_end_date&meta_query%5B1%5D%5Bvalue%5D={{dc:global:date format=“Y-m-d”}}&meta_query%5B1%5D%5Bcompare%5D=%3E%3D&meta_query%5B1%5D%5Btype%5D=DATE&orderby=random

I spoke too soon. The query does not remove the posts outside of the start and end dates. It just displays all of them. Please note I am using ACF for the date fields.

Hey @innovatek,

I’m sorry but we do not provide support for Query String issues because basically, it’s a general WordPress topic. The Query String just accepts WP Query and if your query is incorrect, your setup won’t work. Moreover, it looks like you’re using a 3rd party shortcode [code_snippet id=9 php=true]. We do not provide support for 3rd party systems as well.

If you need us to investigate this issue, you need to consult with our Elite team who does WordPress custom development. Check our Elite service here: https://theme.co/elite

Thanks for understanding.

Hi @christian,
I managed to get both my query strings to work. While a more automated way would be better, its not like I need to keep updating the query, so happy with it now.
To help others, this was my process:

  1. Insert the code (to make the query string) into Code Snippet and paste the snippet’s shortcode into a test page. This plugin is a far easier option than going into the file manager and creating a test.php file. In other forum notes, they say to use var_dump, however, I found that echo removed the unneeded text in the result.
  2. Take the resulting query string and replace the date with {{dc:global:date format=“Ymd”}} - for this query, the date is in two places.
  3. Paste final query string into looper provider.

For my classifieds query string, I only wanted to display posts where the start date was before today and the end date was in the future. The code that works:


<?php

$query = array(
'post_type'      => 'classifieds', // my custom post type    
'post_status'      => 'publish',

'meta_query' => array(
    'relation' => 'AND',
    array(
        'key'     => 'classifieds_start_date',
        'value'   => date("Ymd"),
        'compare' => '<=',
        'type'    => 'DATE'
    ),
    array(
        'key'     => 'classifieds_end_date',
        'value'   => date("Ymd"),
        'compare' => '>=',
        'type'    => 'DATE'
    )
),
'orderby' => 'random',
);     
echo http_build_query( $query );

?>

</body>
</html>

Inserting that Code Snippet’s shortcode into a test page, then replacing the date with the Global Shortcut, it produced the following query string that worked:

post_type=classifieds&post_status=publish&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=classifieds_start_date&meta_query%5B0%5D%5Bvalue%5D={{dc:global:date format=“Y-m-d”}}&meta_query%5B0%5D%5Bcompare%5D=%3C%3D&meta_query%5B0%5D%5Btype%5D=DATE&meta_query%5B1%5D%5Bkey%5D=classifieds_end_date&meta_query%5B1%5D%5Bvalue%5D={{dc:global:date format=“Y-m-d”}}&meta_query%5B1%5D%5Bcompare%5D=%3E%3D&meta_query%5B1%5D%5Btype%5D=DATE&orderby=random

I also created another query string for my event custom posts. This displays only the events with an end date in the future. Code:


<?php

$meta_query[] = array(
    'key'     => 'event_end_date',
    'value'   => date('Ymd'),
    'compare' => '>=',
	'type' => 'DATE'
);

$query = array(
    'post_type'  => 'event',
    'meta_key'   => 'event_end_date',
    'orderby'    => 'meta_value',
    'order'      => 'ASC',
    'meta_query' => $meta_query
);
echo http_build_query( $query );

?>

</body>
</html>

Final working Query String (with Global date shortcode inerted):

post_type=event&meta_key=event_end_date&orderby=meta_value&order=ASC&meta_query%5B0%5D%5Bkey%5D=event_end_date&meta_query%5B0%5D%5Bvalue%5D={{dc:global:date format=“Y-m-d”}}&meta_query%5B0%5D%5Bcompare%5D=%3E%3D&meta_query%5B0%5D%5Btype%5D=DATE

Glad to hear that and thanks for sharing, @innovatek.

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