Last active
October 13, 2017 18:50
-
-
Save DouglasdeMoura/7876ac23c1c0f3c4ff84816701fb6031 to your computer and use it in GitHub Desktop.
WP_Query by date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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