Query String for the WP_QUERY parameters

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

Topic query-string query Wordpress

Category Web


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

'after' => '-24 hours'


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.