Visualizzare i post degli ultimi 7 giorni

13 mag 2013, 15:48:27
Visualizzazioni: 27.7K
Voti: 9

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;?>
2
Commenti

Nota che orderby=highest_rated non è integrato in WordPress. Stai usando qualche plugin per le valutazioni?

RRikesh RRikesh
13 mag 2013 16:07:24

Sì, sto usando un plugin per questo :) Grazie per la tua attenzione!

Swen Swen
13 mag 2013 16:25:06
Tutte le risposte alla domanda 6
2
27

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'
        )
    )
); 
27 nov 2014 17:34:37
Commenti

Questo dovrebbe essere l'approccio a partire dalla 3.7 :-)

Pieter Goosen Pieter Goosen
27 nov 2014 17:41:16

Questo è buono ma recupera i post esattamente 24*7 ore fa. Ad esempio, se attualmente è notte, non recupererà i post pubblicati 7 giorni fa al mattino.

karlosuccess karlosuccess
3 mar 2023 07:23:20
2

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.

13 mag 2013 16:47:54
Commenti

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

Christine Cooper Christine Cooper
26 nov 2014 18:12:49

Sì, questo sarebbe ora il metodo preferito, poiché la risposta è stata scritta prima dell'esistenza del parametro date_query ;-) @ChristineCooper

birgire birgire
27 nov 2014 18:13:00
1

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 );
13 mag 2013 16:03:38
Commenti

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.

Swen Swen
13 mag 2013 16:27:50
0

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 );
28 feb 2018 15:21:41
0

puoi semplicemente usare wp_get_archives()

 wp_get_archives( array( 'type' => 'weekly', 'limit' => 1, 'show_post_count' => 'true' ,'format'=>'option') ); ?>
20 dic 2020 11:14:28
0

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;
}
5 ago 2021 16:08:53