Skip to content

Instantly share code, notes, and snippets.

@DouglasdeMoura
Last active October 13, 2017 18:50
Show Gist options
  • Save DouglasdeMoura/7876ac23c1c0f3c4ff84816701fb6031 to your computer and use it in GitHub Desktop.
Save DouglasdeMoura/7876ac23c1c0f3c4ff84816701fb6031 to your computer and use it in GitHub Desktop.
WP_Query by date
<?php
function dougmoura_query_by_date( $ano = null, $mes = null, $dia = null ) {
// Retorna uma matriz com o dia de hoje. Veja http://php.net/manual/en/function.getdate.php
$hoje = getdate();
if ( null === $ano )
$ano = $hoje['year'];
if ( null === $mes )
$mes = $hoje['month'];
if ( null === $dia )
$dia = $hoje['day'];
return new WP_Query( array(
'date_query' => array(
array(
'year' => $ano,
'month' => $mes,
'day' => $dia,
),
),
) );
}
/** COMO USAR **/
// Pegar um post publicado no dia de hoje, mas de um ano qualquer.
$q = dougmoura_query_by_date( rand( 2012, 2017 ) ); // Veja a função rand(): http://php.net/manual/en/function.rand.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment