Comment faire publier des articles entre une date et aujourd'hui?
-
-
[N'utilisezpas lesmessages de requête!] (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
-
N'utilisezpas query_posts ().Vérifiez ceci -> 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 réponses
- votes
-
- 2012-05-14
MISE À JOUR 23 décembre 2014
Ilexiste unemeilleureméthode utilisant lapropriété
date_query
de la 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 );
ANCIENNE RÉPONSE
Utilisez les Paramètres detemps dans WP_Query ()
Exemple de citation du Codex:
Renvoyer lesmessages des 30 derniersjours:
// 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' );
Modifier (en réponse à la questionmise àjour du PO).
Évitez d'utiliser query_posts . Vouspouvez utiliser latechnique ci-dessuspourmodifier votre requêteprincipale (sous réserve de certaines conditionnelles - sapage d'accueil,est unepage appelée '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' );
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' );
-
D'accord !Lefiltreest doncmaintenant dans `$ query_string`.Mais comment celafonctionne avecmes arguments dans Query_Posts?(Vérifiezmamodification @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 - voir la réponsemise àjour.J'espère que celane vous dérangepas,Moraleida.@Steffi - see updated answer. I hope you don't mind the addition, Moraleida.
- 1
- 2012-05-14
- Stephen Harris
-
vient d'ajouter votre requête actuelle,vouspouvez donc abandonner query_poststout de suite.:) Etmerci @StephenHarrispour lamise àjour rapide!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
-
Merci @moraleida!Incroyable !Juste une chose.Vous avez dit: "Évitez d'utiliser query_posts."Maisilestpréférable d'utiliser `query_posts ()` dans lesfichiers demodèles (comme ** home.php **) que `new WP_Query ()`,non?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
-
Pas vraiment.`query_posts`ne devrait être utilisé quepourmodifier laboucleprincipale -et beaucoup degens soutiennent quemêmepas alors (il y a aussi lefiltre`pre_get_posts`pour cela).Jeme retrouve souvent àn'utiliser que `WP_Query` ou`get_posts`pourtoutesmes requêtes carelles sont autonomeset peuvent être utiliséesplusieursfois sansinterférer avec quoi que ce soit d'autre.Vérifiez les réponses liées à vos commentairespour uneexplication approfondie.:)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
Àpartir de 3.7,vouspouvez utiliser date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
Les argumentspassés ressembleraient donc à:
$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
Si vous souhaitez obtenir des articlesentre deux dates,utilisez lesparamètres avantet après dans leparamètre 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' );
Est-ce unmoyen depublier des articlesentre une dateet aujourd'hui avec
query_posts()
?Exemple:tous les articlespubliés depuis le 2012-04-01
Merci
MODIFIER:
Comment ajouter la date defiltrage sur lesmessages de cette requête?