Come ottenere i post pubblicati tra una data e oggi?
C'è un modo per ottenere i post pubblicati tra una data e oggi con query_posts()
?
Esempio: Tutti i post pubblicati dal 2012-04-01
Grazie
MODIFICA:
Come aggiungere il filtro per data a questa Query Posts?
query_posts( array(
array('post'),
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-image')
)
),
'cat' => '-173',
'post_status' => 'publish'
) );

AGGIORNAMENTO 23 dicembre 2014
Esiste un metodo migliore utilizzando la proprietà date_query
della classe WP_Query
:
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-image' )
)
),
'cat' => '-173',
'post_status' => 'publish',
'date_query' => array(
'column' => 'post_date',
'after' => '- 30 days'
)
);
$query = new WP_Query( $args );
RISPOSTA VECCHIA
Utilizza i Parametri Temporali in WP_Query()
Citando l'esempio dal Codex:
Restituisce i post degli ultimi 30 giorni:
// Prende la tua query corrente, a cui verrà aggiunta la parte di filtraggio.
$query_string = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-image' )
)
),
'cat' => '-173',
'post_status' => 'publish'
);
// Crea una nuova funzione di filtraggio che aggiungerà la nostra clausola WHERE alla query
function filter_where( $where = '' ) {
// post negli ultimi 30 giorni
$where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-30 days' ) ) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
Modifica (in risposta all'aggiornamento della domanda dell'OP).
Evita di usare query_posts. Puoi usare la tecnica sopra per modificare la tua query principale (soggetta ad alcune condizioni aggiuntive - è la home page, è una pagina chiamata 'foobar' ecc.):
function wpse52070_filter_where( $where = '' , $query ) {
if( $query->is_main_query() && is_page( 'foobar' ) ){
// post negli ultimi 30 giorni
$where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-30 days' ) ) . "'";
}
return $where;
}
add_filter( 'posts_where', 'wpse52070_filter_where' );

Ok! Quindi il filtro è ora in $query_string
. Ma come funziona con i miei argomenti in Query_Posts? (Controlla la mia modifica @Moraleida)

@Steffi - vedi risposta aggiornata. Spero non ti dispiaccia l'aggiunta, Moraleida.

ho appena aggiunto la tua query attuale, così puoi eliminare subito query_posts. :) E grazie @StephenHarris per l'aggiornamento rapido!

Grazie @moraleida ! Fantastico ! Solo una cosa. Hai detto: "Evita di usare query_posts." Ma è meglio usare query_posts()
nei file template (come home.php) piuttosto che new WP_Query()
, no?

Non proprio. query_posts
dovrebbe essere usato solo per modificare il loop principale - e molti sostengono che nemmeno in quel caso (c'è anche il filtro the pre_get_posts
per quello). Spesso mi ritrovo a usare solo WP_Query
o get_posts
per tutte le mie query poiché sono indipendenti e possono essere usate più volte senza interferire con altro. Dai un'occhiata alle risposte linkate nei tuoi commenti per una spiegazione approfondita. :)

Se desideri ottenere i post tra due date, utilizza i parametri before e after all'interno del parametro date_query,
$query_string = array(
'post_type' => 'post',
'date_query' => array(
'column' => 'post_date',
'after' => '2012-04-01',
'before' => '2012-04-30'
),
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-image')
)
),
'cat' => '-173',
'post_status' => 'publish'
);

A partire dalla versione 3.7 puoi utilizzare date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
Quindi gli argomenti passati appariranno così:
$query_string = array(
'post_type' => 'post',
'date_query' => array(
'after' => '2012-04-01'
),
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-image')
)
),
'cat' => '-173',
'post_status' => 'publish'
);
