0

I have a custom post type called promotions.

In each promotions' post meta, I store two fields, promo_start_date and promo_end_date.

The idea is to only show promotions on the frontend, where the

current_date >= promo_start_date AND current_date <= promo_end_date

For example, if the promotion has the value of 2022-12-18 in the promo_start_date and 2023-01-08 in the promo_end_date, it will only show up on the frontend on the 2022-12-18 and then display till the 2023-01-08.

I was wondering how possible it is to achieve this with meta_query.

$args = [
    'paged' => 1
    'post_type' => 'promotions',
    'posts_per_page' => 15,
    'meta_query' => [
        [
            'key' => 'promo_start_date'
            'value' => 'CURRENT_DATE'
            'type' => 'DATE'
            'compare' => '>='
        ],
        [
            'key' => 'promo_end_date'
            'value' => 'CURRENT_DATE'
            'type' => 'DATE'
            'compare' => '<='
        ],
    ],
];

I can easily implement this in raw sql but unfortunately I am tweaking a filter for a plugin in order to achieve the result that I need.

The filter is coming from the Search and Filter plugin and its documentation can be found here

And here is an example of the code that I am trying to implement

function me_sf_edit_query_args($query_args, $sfid) {
    // if search form has specified ID, change query args
    if ($sfid == 11000) {
        $query_args['meta_query'] = [
            [
                
            ]
        ];
        dd($query_args);
    }

    return $query_args;
}
add_filter('sf_edit_query_args', 'me_sf_edit_query_args', 20, 2);

Thank you

6
  • Do you mean 'relation' => 'and' property? or are you having issues with the values? You can check it here developer.wordpress.org/reference/classes/wp_query/… Commented Dec 14, 2022 at 12:37
  • @Buttered_Toast No, but I edited the question and added an explanation to help provide more context Commented Dec 14, 2022 at 12:44
  • What date format are you passing? I tend to use the full MySQL date time format Y-m-d H:i:s Commented Dec 14, 2022 at 13:01
  • Just Y-m-d, because that information is coming from API Commented Dec 14, 2022 at 13:03
  • 1
    there are no filters in your question, please include the filter, and note that while meta_query might be able to do what you want, there is no guarantee that this is still true when the plugin is involved, and the plugin may have special requirements that need to be handled that require specialised knowledge of the plugin. As a result any answer you get here may be correct but unusable Commented Dec 14, 2022 at 13:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.