Articles of date query

Date and Category query with filter

I am a little confused by this. I have a query that finds posts within a category and date. I can filter the categories, but I have no idea how to filter by date. The query works. I see all of the posts I have queried, but I don’t know how to filter the date. […]

How to get old posts instead of recent with WP_Query?

I’m doing a WP_Query with a date_query parameter after, and I want the nearest post after that date, but I get the farest ones in the future from the date. Is there any parameter to control this? something like ‘near_to’ => ‘past/future’, or WP_Query always retrieve the most recent? This is my code: $my_query = […]

date_query is showing duplicate results

I’m trying to get the posts that was updated from today, yesterday and 2 days ago but 2 days ago is showing the result of yesterday. What seems wrong with my code? Today and Yesterday is working fine. And also the post that query showing from yesterday and 2 days ago are only updated once […]

Why the 'date_query' is not working in 'pre_get_posts' hook?

I need to output popular posts in a blog using several params: Not older than one week; Rating is over than 250; Order by date Here is my code: function evanre_custom_order_query( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } $query_var_show = get_query_var( ‘show’ ); if ( empty( $query_var_show ) ) […]

If no posts exist for current day and month, show the next available day of posts

I’m writing an on this day function which should show the X number of posts for the current day and month. How can I add a fallback to the query if there are no posts, to show the next available day of posts? These are the arguments I’m passing to the current day query. $onThisDay_args […]

Only display post if published in last 24 hours?

All I’m trying to do here is a simple custom loop using WP_Query to display posts if they are published in the last 24 hours; otherwise, a message appears telling the user to check back soon for fresh posts. I’m trying to use the date_query parameter in my arguments but I’m getting unexpected results…at first […]

Get posts for last working week in WP_Query

With the date_query in wordpress I was trying to get posts for the last working week (Monday to Friday) I was using: $base_array = array( ‘posts_per_page’ => -1, ‘fields’ => ‘ids’, ‘post_type’ => ‘cpt’, ‘post_status’ => array(‘publish’), ‘date_query’ => array( ‘before’ => ‘next Saturday’, ‘after’ => ‘last Monday’ ) ); $base = get_posts($base_array); But today, […]

Date_query problems

I need to modify search query in my theme to add date_query with ‘after’ param based on option value from form select. Here’s what I got so far: Inside form my select looks like: <select class=”select-date” id=”select-date”> <option value=”5″>For the whole time</option> <option value=”1″>For one day</option> <option value=”2″>For 3 days</option> <option value=”3″>For week</option> <option value=”4″>For […]

Get only modified posts

I’m using this code for generating a Feed from lasted modified post mysqli_query( $conn, “SELECT * FROM wp_posts WHERE post_status = ‘publish’ AND post_type = ‘post’ AND DATE(post_modified) > DATE(post_date) ORDER BY post_modified DESC LIMIT 50” ); and it works perfect, now I need to reproduce in a WordPress plugin and I use this code: […]

Date Query to Pull Current and Future Posts

I’m trying to use date_query to query posts with todays date or a future date. Think events, I don’t want to show any posts which has a date before todays date. I’m using pre_get_posts and setting the query like so: $query->set( ‘post_status’, array( ‘publish’, ‘future’ ) ); $query->set( ‘date_query’, array( array( ‘year’ => date( ‘Y’ […]