Visualizzare i post degli ultimi 7 giorni
Sto cercando di visualizzare i 5 post più votati dell'ultima settimana (7 giorni) sul mio sito web, tuttavia non riesco a capire come visualizzarli.
Ecco cosa ho ottenuto finora ma non sembra funzionare:
<?php $slider_query = new WP_Query('posts_per_page=5&cat=3&orderby=highest_rated&order=desc'); ?>
<?php
$mylimit = 7 * 86400; // giorni * secondi per giorno
while ($slider_query->have_posts()) : $slider_query->the_post();
$post_age = date('U') - get_post_time('U');
if ($post_age < $mylimit) {
?>
//Il Post
<?php } ?>
<?php endwhile;?>

Oltre alla soluzione di birgire, a partire da WordPress 3.7, puoi utilizzare i parametri di data.
I tuoi argomenti dovrebbero apparire così per filtrare gli articoli degli ultimi 7 giorni:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
// Utilizzo di date_query per filtrare gli articoli dell'ultima settimana
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);

Penso che questo problema sia già stato risolto molte volte qui su WordPress Answers.
Potresti anche dare un'occhiata agli esempi nella sezione Parametri temporali nel Codex per WP_Query
.
Ecco due esempi (leggermente modificati per le tue esigenze)
Esempio 1:
// Crea una nuova funzione di filtraggio che aggiungerà la nostra clausola WHERE alla query
function filter_where( $where = '' ) {
// post degli ultimi 7 giorni
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$slider_query = new WP_Query('posts_per_page=5&cat=3&orderby=highest_rated&order=desc');
remove_filter( 'posts_where', 'filter_where' );
Esempio 2:
// Crea una nuova funzione di filtraggio che aggiungerà la nostra clausola WHERE alla query
function filter_where( $where = '' ) {
// post dal 1 maggio all'8 marzo 2013
$where .= " AND post_date >= '2013-05-01' AND post_date < '2013-05-8'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$slider_query = new WP_Query('posts_per_page=5&cat=3&orderby=highest_rated&order=desc');
remove_filter( 'posts_where', 'filter_where' )
supponendo che tu abbia già gestito questo orderby=highest_rated
con qualche plugin come descrivi nel commento sopra.

Mi chiedo se sia meglio usare 'date_query'? Aggiungendo quanto segue agli argomenti di wp_query: 'date_query' => array( array('after' => '1 week ago')

Dalla sezione WP_Query
Parametri Temporali:
Restituisce i post solo per la settimana corrente:
$week = date('W');
$year = date('Y');
$query = new WP_Query( 'year=' . $year . '&w=' . $week );

Ciao. Non l'ho ancora testato, ma sono abbastanza sicuro che non sia esattamente quello che cerco. Questo probabilmente elenca i post pubblicati nella settimana 13, ad esempio. Quello che voglio sono i post che sono stati pubblicati negli ultimi 7 giorni. Quindi, ad esempio, da mercoledì 8 maggio fino a mercoledì 1 maggio.

Funziona per me in questo modo, per mostrare i post degli ultimi 7 giorni in base al numero di visualizzazioni e ordinarli per conteggio delle visualizzazioni in ordine DESC.
$date_range = strtotime ( '-7 day' );
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '10',
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
array(
'after' => array(
'year' => date('Y', $date_range ),
'month' => date('m', $date_range ),
'day' => date('d', $date_range ),
),
)
)
);
$query = new WP_Query( $args );

puoi semplicemente usare wp_get_archives()
wp_get_archives( array( 'type' => 'weekly', 'limit' => 1, 'show_post_count' => 'true' ,'format'=>'option') ); ?>

Utilizzo più semplice della query SQL con l'hook WordPress posts where
function getStartAndEndDate($week, $year) {
$dto = new DateTime();
$dto->setISODate($year, $week);
$ret['week_start'] = $dto->format('Y-m-d');
$dto->modify('+6 days');
$ret['week_end'] = $dto->format('Y-m-d');
return $ret;
}
add_filter( 'posts_where', 'wpse29897_no_parents', 10, 2 );
function wpse29897_no_parents( $where, $query )
{
if( isset( $query->query_vars['post_type'] ) && 'menu_plans' == $query->query_vars['post_type'] )
{
if( '' != $where )
{
$currentdate = date('Y-m-d');
$currdate = new DateTime($currentdate);
if($_GET['weekchk']){
$currentweek = $_GET['weekchk'];
}else{
$currentweek = $currdate->format("W");
}
$week_array = getStartAndEndDate($currentweek,date('Y'));
$ws = "'".$week_array['week_start']."'";
$we = "'".$week_array['week_end']."'";
$where .= " AND post_date >= ".$ws." AND post_date < ".$we."";
}
else
{
$where .= " AND post_date >= '2020-07-28' AND post_date < '2020-08-4'";
}
}
return $where;
}
