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.
- 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.
- 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. - 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