0

I have this line of code:

$args = array(
    'date_query' => array(
        array(
            'after'     => 'January 1st, 2015',
            'before'    => 'February 15th, 2015',
            'inclusive' => true,
        ),
    ),
);
$query = new WP_Query( $args );

This code will return all the post published between Jan 1st and Feb 15th. My question is can I somehow create a link to the results of this query?

For example I can go to

localhost/wordpress?monthnum=1 

and get all posts from January, but is there a link, or can I create custom link that will accept the parameters of my date_query, so I can display all the posts that match my query?

Basically I just want to show all posts published between specific dates on a separate page, and I want a link to that page, but I don't know if such thing is possible.

Just to be clear, I just want to know is there a link such as:

localhost/wordpress?after=January1st2015&before=February15th2015

available in Wordpress so I can quickly access the query results, or is there a way to create such a link?

1

You can use $_GET to get the query string values and then pass it in the $args to create a dynamic WP_Query

$after = $_GET['after'];
$before = $_GET['before'];

and then in the $args

if ($after && $before) {
    $args = array(
        'date_query' => array(
            array(
                'after'     => $after,
                'before'    => $before,
                'inclusive' => true,
            ),
        ),
    );
} else {
    // default args
}
$query = new WP_Query( $args );
| improve this answer | |

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.