0

I don't know how to turn some WP QUERY parameters into url query strings. Most of them are straight forward, but there are some I don't know how to get working. For example, for these WP_QUERY parameters:

$args = array(
    'post_type' => 'post',
    's' => 'keyword',
);

the query string for this is: ?s=keyword&post_type=post

What is the query string for the 'after' parameter with value '24 hours ago'. For example, this WP_QUERY gets posts created in the last 24 hours:

$args = array(
    'date_query' => array(
        array(
            'after'  => '24 hours ago',
        ),
    ),
);

But I don't know the query string for it. This doesn't work:

?after=24%20hours%20ago

Any help appreciated.

0

PHP function http_build_query(); would return different string for this array. It won't remain readable like an array but, I believe, it will work as parameter to WP_Query.

build_query is the WordPress equivalent for the same functionality.

Turns out that build_query is a bit different from http_build_query.

http_build_query returned date_query%5B0%5D%5Bafter%5D=24+hours+ago and build_query returned date_query%5B0%5D%5Bafter%5D=24 hours ago. I guess, later would work for WP_Query.

  • 1
    Thanks for your help. I just tried them but they don't work either. All the other date parameters inside the 'date_query' work just by adding year=value&month=value&w=value&day=value&hour=value. As stated here: codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters. I thought just by using after=value and before=value would work too but they don't. – Andy Oct 20 '16 at 23:49
0

The codex says you can use any strtotime() compatible string so you could use:

'after' => '-24 hours'

  • Thanks for your help. I need it to be a URL query string, not as an array. in the form of ?key=value&key=value. If you visite codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters and scroll down to where it says 'Returns posts for today' it shows how to write it. year=value&monthnum=value&day=value all work. But after=value doesn't work. – Andy Oct 20 '16 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.