Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1050 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query posts published after a certain date in Wordpress

#1
I'm trying to write a `WP_Query` where I'm calling only posts that have been posted after march 2012. I can successfully call posts that are just in March 2012, but struggling to do 'from March 2012 onwards'.

$current_year = date('2012');
$current_month = date('>3'); // This doesn't work
$current_month = date('3'); // This DOES work

$custom_query = new WP_Query("year=$current_year&monthnum=$current_month&order=ASC&posts_per_page=-1");

Am I missing something simple, or does this have to get more complicated?
Reply

#2
The "Time Parameters" section in

[To see links please register here]

has a note about date ranges. Using the same technique:

$query_string = "order=ASC&posts_per_page=-1";

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
$where .= " AND post_date >= '2012-03-01'";
return $where;
}

add_filter( 'posts_where', 'filter_where' );
$custom_query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
Reply

#3
Since WordPress version 3.7, there's the WP_Query argument [`date_query`][1] that works perfectly for this type of query.

[As you can see in the Codex][1], you can specify a date query with the `after` argument. The `after` can either be a strtotime()-compatible string, or an array of 'year', 'month', 'day' values.

For your example, something like the following should work:

$args = array(
'posts_per_page' => -1,
'date_query' => array(
'after' => array(
'year' => 2012,
'month' => 3,
'day' => 1,
),
),
);
$custom_query = new WP_Query( $args );

Or with a strtotime()-string:

$args = array(
'posts_per_page' => -1,
'date_query' => array( 'after' => '2012-03-01' ),
);
$custom_query = new WP_Query( $args );

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through