Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda