Come ottenere i post pubblicati tra una data e oggi?
-
-
[Non utilizzarepost di query!] (Http://wordpress.stackexchange.com/q/50761/9364) :)[Don't use query posts!](http://wordpress.stackexchange.com/q/50761/9364) :)
- 0
- 2012-05-14
- Stephen Harris
-
Non utilizzare query_posts ().Controlla questo -> http://wordpress.stackexchange.com/a/1755/7890Don't use query_posts(). Check this -> http://wordpress.stackexchange.com/a/1755/7890
- 0
- 2012-05-14
- moraleida
-
3 risposta
- voti
-
- 2012-05-14
AGGIORNAMENTO 23 dicembre 2014
Esiste unmetodomigliore che utilizza laproprietà
date_query
della classeWP_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 );
VECCHIA RISPOSTA
Utilizzai Parametritemporaliin WP_Query ()
Citando unesempio dal Codex:
Restituiscii post degli ultimi 30giorni:
// This takes your current query, that will have the filtering part added to. $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' ); // Create a new filtering function that will add our where clause to the query function filter_where( $where = '' ) { // posts in the last 30 days $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 alla domanda aggiornata del PO).
Evita di utilizzare query_posts . Puoi utilizzare latecnica soprapermodificare latua queryprincipale (soggetto ad alcuni condizionali extra - è la homepage,è unapagina chiamata "foobar"ecc.):
function wpse52070_filter_where( $where = '' , $query ) { if( $query->is_main_query() && is_page( 'foobar' ) ){ // posts in the last 30 days $where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-30 days' ) ) . "'"; } return $where; } add_filter( 'posts_where', 'wpse52070_filter_where' );
UPDATE December 23 2014
There is a better method using
date_query
property ofWP_Query
class:$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 );
OLD ANSWER
Use the Time Parameters in WP_Query()
Quoting example from the Codex:
Return posts from the last 30 days:
// This takes your current query, that will have the filtering part added to. $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' ); // Create a new filtering function that will add our where clause to the query function filter_where( $where = '' ) { // posts in the last 30 days $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' );
Edit (in response to the OP's updated question).
Avoid using query_posts. You can use the above technique to alter your main query (subject to some extra conditionals - is home page, is a page called 'foobar' etc. ):
function wpse52070_filter_where( $where = '' , $query ) { if( $query->is_main_query() && is_page( 'foobar' ) ){ // posts in the last 30 days $where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-30 days' ) ) . "'"; } return $where; } add_filter( 'posts_where', 'wpse52070_filter_where' );
-
Ok !Quindiilfiltro è orain "$ stringa_query".Ma comefunziona coni miei argomentiin Query_Posts?(Controlla lamiamodifica @Moraleida)Ok ! So the filter is now in `$query_string`. But how it works with my args in Query_Posts ? (Check my edit @Moraleida)
- 0
- 2012-05-14
- Steffi
-
@Steffi - vedi risposta aggiornata.Sperononti dispiaccia l'aggiunta,Moraleida.@Steffi - see updated answer. I hope you don't mind the addition, Moraleida.
- 1
- 2012-05-14
- Stephen Harris
-
hai appena aggiunto latua query corrente,cosìpuoi abbandonare subito query_posts.:) Egrazie @StephenHarrisperil rapido aggiornamento!just added your current query, so you can ditch query_posts right away. :) And thanks @StephenHarris for the quick update!
- 1
- 2012-05-14
- moraleida
-
Grazie @moraleida!Sorprendente !Solo una cosa.Hai detto: "Evita di usare query_posts".Ma èmeglio usare `query_posts ()`nei file deimodelli (come ** home.php **) che `new WP_Query ()`,no?Thank you @moraleida ! Amazing ! Just one thing. You said : "Avoid using query_posts." But it is better to use `query_posts()` in templates files (such as **home.php**) than `new WP_Query()`, no ?
- 0
- 2012-05-14
- Steffi
-
Nonproprio.`query_posts` dovrebbeessere usato soloper alterareil cicloprincipale -e moltepersone sostengono chenemmeno allora (c'è ancheilfiltro`pre_get_posts`per quello).Spessomi trovo a utilizzare solo `WP_Query` o`get_posts`pertutte lemie querypoiché sonoindipendentie possonoessere utilizzatipiù volte senzainterferire connient'altro.Controlla le risposte collegate aituoi commentiper una spiegazione approfondita.:)Not really. `query_posts` should be used only to alter the main loop - and a lot of people argue that not even then (there's `the pre_get_posts` filter for that too). I often find myself using only `WP_Query` or `get_posts` for all my queries as they're standalone and can be used multiple times w/o interfering with anything else. Check the linked answers on your comments for a thorough explanation. :)
- 0
- 2012-05-14
- moraleida
-
- 2014-07-31
Apartire dalla versione 3.7puoi utilizzare date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
Quindigli argomentipassati sarebbero simili a:
$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' );
As of 3.7 you can use date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
So the args passed would look like:
$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' );
-
- 2019-05-27
Se desideri ricevereposttra due date,utilizzai parametriprimae doponelparametro 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' );
If you wish to get posts between two dates, then use the before and after parameters in the date_query parameter,
$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' );
È unmodoper ottenerepostpubblicatitra una datae oggi con
query_posts()
?Esempio:tuttii postpubblicati dal 2012-04-01"
Grazie
MODIFICA:
Come aggiungere la data delfiltro a questipost di query?