Come ottenere i post pubblicati tra una data e oggi?

14 mag 2012, 15:59:24
Visualizzazioni: 76.8K
Voti: 16

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'
) );
2
Commenti

Non usare query posts! :)

Stephen Harris Stephen Harris
14 mag 2012 16:21:10

Non usare query_posts(). Controlla qui -> http://wordpress.stackexchange.com/a/1755/7890

moraleida moraleida
14 mag 2012 16:22:18
Tutte le risposte alla domanda 3
5
36

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' );
14 mag 2012 16:08:15
Commenti

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

Steffi Steffi
14 mag 2012 16:16:02

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

Stephen Harris Stephen Harris
14 mag 2012 16:30:18

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

moraleida moraleida
14 mag 2012 16:32:14

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?

Steffi Steffi
14 mag 2012 16:43:28

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. :)

moraleida moraleida
14 mag 2012 17:00:51
0

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'
);
27 mag 2019 06:31:35
0

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'
);
1 ago 2014 00:41:16