Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Query post_meta date instead of post_date_gmt

I'm limiting my query to use show post from 6 months ago which works fine.

But I need it to be based on a date that is in the post_meta table instead of 'post_date_gmt'.

In my case I have meta_keys are called payment_date and the values are of course a date like 31-10-2016 for example.

$months_ago = 6;
$args = array(
'date_query' => array(
    array(
        'column' => 'post_date_gmt',
        'after'  => $months_ago . ' months ago',
        )
    ),
'numberposts'   => -1
);
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Check it here: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

If you scroll the examples, you'll see:

Display posts where the custom field key is a set date and the custom field value is now. Displays only posts which date has not passed.

$args = array(
    'post_type'    => 'event',
    'meta_key'     => 'event_date',
    'meta_value'   => date( "Ymd" ), // change to how "event date" is stored
    'meta_compare' => '>',
);
$query = new WP_Query( $args );

In your case you could put something like: date( "dmY", strtotime( '6 months ago' ) )

about 4 years ago · Santiago Trujillo Report

0

Try this code:

$sixmonthagodate = date( "d-m-Y", strtotime('-6 Months') );

$args = array(
    'post_type'    => 'post',
    'posts_per_page' => -1,
    'meta_key'     => 'payment_date',
    'meta_value'   => $sixmonthagodate,
    'meta_compare' => '>',
);
$query = new WP_Query( $args );

check that if your post_type is true in your WordPress site.

about 4 years ago · Santiago Trujillo Report

0

First thing first date_query works on post_date_gmt. If you want to query post from meta fields then you have to use meta_query.

Here is a sample code:

$months_ago = 6;
$args = [
    //...
    //...
    'posts_per_page' => -1, // Unlimited posts
    //...
    //...
    'meta_query' => [
        'relation' => 'AND',
        [
            'key' => 'payment_date',
            'value' => date('d-m-Y', strtotime($months_ago . ' months ago')), //<-- Converting date into your custom date format
            'compare' => '>', //you can also use <=, >=, <, etc..
            'type' => 'DATE'
        ],
    ]
];

$query = new WP_Query($args);

if ($query->have_posts()) :
    /* Start the Loop */
    while ($query->have_posts()) : $query->the_post();

    //you post

    endwhile;
endif;

The above code should work for you.

Related Question:

  • Hide past events posts based on custom date field
  • Orderby ACF custom field date don't work

Hope this helps!

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!