How to add date_query to meta_query array

I am building a meta query array and all fields for the user are optional so some might not have any data. But there is also an option which requires a date query. I am still checking to see if this is working but not sure as of yet. Is this correct and if not, what would be the right way to do it? Not sure if there needs to be an OR condition in here somewhere. $meta_query = []; …
Category: Web

date_query empty results with custom post type

I am trying to use get_posts to return data between certain dates but I always get an empty result even though there is a lot of data. This is for a custom post type. $args = array( 'post_type' => 'bookings', 'fields' => 'ids', 'post_status' => 'any', 'posts_per_page' => -1, 'date_query' => array( array( 'after' => '2022-01-01', 'before' => '2022-06-01', 'inclusive' => true, ), ) ); $data = get_posts($args); die(var_dump($data)); I also tried something like the below to get a specific …
Category: Web

date_query won't accept day parameter, but will accept month and year for a custom post

I have a frustrating issue. I am using the date_query as referenced in the Codex: <?php $today = getdate(); $args = array( 'post_type' => 'Lighting', 'post_status' => 'publish', 'posts_per_page' => 1, 'date_query' => array( array( 'year' => $today['year'], 'month' => $today['mon'], 'day' => $today['mday'], ), ), ); $the_query = new WP_Query( $args ); While I can display custom posts as expected if I exclude 'day', I get no posts when it is included in the query. I am using Advanced …
Category: Web

WooCommerce Shop Orders by Date

I need to get WooCommerce orders and filter it between dates but its not working. Here's my code: $start = '2022-03-00'; $end = '2022-12-20'; $args = array( 'post_type' => 'shop_order', 'posts_per_page' => -1, 'after' => $start, 'before' => $end, 'meta_query' => array( array( 'key' => '_order_send_time', 'value' => array($start, $end), 'compare' => 'BETWEEN', 'type' => 'DATE' ), array( 'key' => '_fb_pickup_branch', 'value' => "900", 'compare' => 'like', ) ) ); // query $events_query = new WP_query(); $events_query->query($orderArgs);
Category: Web

Delete all posts with a specific custom field using meta_query

I'm building a listings website using WooCommerce. A listing is either set to live or sold inside of a custom field key status Once a listing is sold then the custom field status is updated with sold. This allows the listing to be hidden from the website, yet still able to be viewed directly, just in case users still click a link and end up on the listing page. I am looking to automatically delete (or move to trash for …
Category: Web

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. It would help if I knew what was meant to appear in the url The search for categories returns /blog/?category=promotions What should a date filter …
Category: Web

date_query in pre_get_posts out of memory

I'm trying to apply a Datefilter for specific categories. Wordpress crashs with a out of memory error. Is this a server problem or is my query wrong? At first I tried query -> (...) but with approach I get an error, cause of too few arguments. While using query = new WP_Query... I get the out of memory error. Thank you for your help. My code: function test_filter( $query ) { $options = get_option('dh_options'); $cats = "54,55,56"; if ($options['checkbox_usr_q'] == …
Category: Web

Very complex post query

I need to create a very complex post query, but I don't know if such a query can exist, or if I have to write PHP code to combine the results of two different queries... Let me please describe the situation first, and then write what I've done so far. So, I need to return results from a CPT. The CPT has its own taxonomy, where I've inserted the necessary terms. The terms have to do with different genre of …
Category: Web

Most viewed post for the last 2 days using WP_Query

Hello I have code intended to get the most viewed post for last 2 days and it seems like not working or maybe my code is just incorrect. I appreciate any help. Thanks. $args = array( 'post_type' => 'post', 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'date_query' => array( array( 'after' => '2 days ago', ) ) ); $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<ul class="posts-list">'; while ( $the_query->have_posts() ) { $the_query->the_post(); …
Category: Web

Need to show 7 posts from actual date

I have specific category, in what I have a lot of posts. Every post starts from, for example 21 october 2019. I have this code to show posts in the correct sequence: class GoroskopController { static function get_data($parent_id, $number, $heading, $heading_color = 'dark') { $loop = new \WP_Query(GoroskopModule::loop_args($parent_id, $number)); if ($loop->have_posts()) : echo '<div class="goroskop-block mb30">'; echo '<div class="block-heading-color-small -' . $heading_color . ' font-sans">' . $heading . '</div>'; while ($loop->have_posts()) : $loop->the_post(); $this_date = self::this_gor(get_the_title()); include "templates/block.php"; endwhile; wp_reset_query(); …
Category: Web

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: $lastupdated_args = array( 'paged' => $paged, 'orderby' => 'modified', 'ignore_sticky_posts' => '1' ); but in this case it shows all posts ordered my latest modification …
Category: Web

Why I can't 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 = new WP_Query(array( 'post_type' => 'event', 'posts_per_page' => 1, 'date_query' => array( 'after' => date('Y-m-d h:i',time()) ) )); I have an events site, and I want …
Category: Web

Add to previous posts under post

Goal: After each post (that contains under a certain amount of words), display the two posts that were published before it in that category/tab. I found this on stack, where they do something similar, but they're displaying the 2 most recent posts. I want to display the 2 that were published before the currently-read article, though. Is there a way to modify this code?
Category: Web

WP Query date_query with several date range

I am trying to query my post for several date ranges which are specifically posts that are published in 24 hours, within 7 days, posts that are two weeks old, and posts older than 2 weeks. I am able to easily query the posts if I am trying to query a specific date range for example, posts from 24 hours. What I am struggling with right now is I am not certain how to query the posts when two date …
Category: Web

Get posts only from current calendar week

I'm building a weekly leaderboard and trying to display posts from on the current calendar week Monday to Sunday. I've tried the code below, but it's only retrieving a post from today (Tuesday 1st January) and no posts from the day before (Monday 31st December). Any ideas? Thanks! $args = array( 'date_query' => array( array( 'year' => date( 'Y' ), 'week' => date( 'W' ), ), ), 'post_type' => 'ride', 'posts_per_page' => 99, 'order' => 'DEC', ); $leaders = new …
Category: Web

How to combine tax_query and date_query in Wordpress

I'm using the following query to extract all products which are not in the 'Service' product category and order them by a custom meta field '_vintage_age'. $q->set( 'tax_query', array(array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array( 'Service' ), 'operator' => 'NOT IN' ))); $q->set('meta_query',array(array( 'vtp_age' => array( 'key' => '_vintage_age', 'value' => array(1,2,3), 'compare' => 'IN' )))); $q->set('orderby',array( 'vtp_age' => 'ASC', 'date' => 'ASC' )); I now need to extend this query to EXCLUDE products which have been …
Category: Web

Limit RSS feed to previous calendar month

I want to filter my RSS feed to only show posts in the previous calendar month. I have this code in my theme functions file but it is not working? // filter the RSS feeds to show only the last calendar month $lastMonthNumber = date( 'n', current_time( 'timestamp' ) ) - 1; // work out the previous month number function feedFilter($query) { if ($query->is_feed) { $query->set('date_query', array( array( 'month' => $lastMonthNumber //'month' => 5 ) )); } return $query; } …
Category: Web

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 month</option> </select> Values must filter content based on date posts were posted. For example selected value of 3 gives us posts published in the last …
Category: Web

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 = array( 'post_type' => array('on-this-day'), 'posts_per_page' => $postlimit, 'date_query' => array( array( 'month' => date( 'n', current_time( 'timestamp' ) ), 'day' => date( 'j', current_time( …
Category: Web

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 ) ) { return; } if ( 'popular' === $query_var_show ) { $query->set( 'orderby', 'date' ); $query->set( 'meta_query', array( array( 'key' => 'blt_upvotes', 'value' => 250, 'compare' …
Category: Web

About

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