> Faqs > Código para mostrar los posts más vistos de la última semana en WordPress

Código para mostrar los posts más vistos de la última semana en WordPress

Un saludo,

Querría saber si alguien me podría ayudar con este código para que mostrara los 5 posts más vistos en la ÚLTIMA SEMANA. Actualmente este código está programado para que se muestren los 5 posts más vistos históricamente:

function wpb_set_post_viewsnew($postID) {
    $count_key = 'wpb_post_viewsnew_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
//To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

function popularesnew_func( $atts ) {
    extract( shortcode_atts( array(
        'url' => 'https://staging.gasteizhoy.com',
    ), $atts ) );

		ob_start();?>

		<section class="fondo-reportajes">

			<div class="m0auto ">

			<div class="h2bg" style="background-color: #009836; margin-bottom: 10px;"><h2 class="h2blogs h2reportajes" style="color: white;">Lo + leído</h2></div>
			<!--<div class="lineas lblogs amp-wp-5bed16a" data-amp-original-style="margin-bottom: 10px; margin-top: 5px;"><div class="roja"></div><div class="verde"></div></div>-->
			</div>	

			<div class="row clearfix fullvw" style="margin: 0 auto;">
	
			
	
			<?php 
				$popularpost = new WP_Query( array( 'posts_per_page' => 5, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'date_query' => array('before' => 'next Saturday','after'  => 'last Monday'  ) ));
				while ( $popularpost->have_posts() ) : $popularpost->the_post(); $i++?>
			
			<div class="trending-block">
					
			<div class="trending-number">
			<h2><?php echo $i;?></h2>
			</div>

			<div class="trending-title">
			<a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><h2><?php the_title(); ?></h2></a>
			</div>
				</div>
				<?php $do_not_duplicate[] = $post->ID; ?>
				<?php endwhile;?>

		
			</div>
		</section>

		<?php
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
};
add_shortcode( 'popularesnew', 'popularesnew_func' );

Respuestas

Este código debería mostrarte los 5 posts más vistos en la última semana, en lugar de los más vistos históricamente.

Para orientarte antes, necesitas ajustar la consulta WP_Query en tu código. Tienes un problema en la parte que establece el rango de fechas. poeque los valores de before y after no están correctamente configurados para referirse a la semana pasada.

A ver si esto te funciona.......

function wpb_set_post_viewsnew($postID) {
    $count_key = 'wpb_post_viewsnew_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count == ''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    } else {
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

// To keep the count accurate, let's get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

function popularesnew_func( $atts ) {
    extract( shortcode_atts( array(
        'url' => 'https://staging.gasteizhoy.com',
    ), $atts ) );

    ob_start(); ?>

    <section class="fondo-reportajes">
        <div class="m0auto ">
            <div class="h2bg" style="background-color: #009836; margin-bottom: 10px;">
                <h2 class="h2blogs h2reportajes" style="color: white;">Lo + leído</h2>
            </div>
        </div>

        <div class="row clearfix fullvw" style="margin: 0 auto;">

        <?php 
            $popularpost = new WP_Query( array( 
                'posts_per_page' => 5, 
                'meta_key' => 'wpb_post_viewsnew_count', // Correct meta key
                'orderby' => 'meta_value_num', 
                'order' => 'DESC',
                'date_query' => array(
                    array(
                        'after' => '1 week ago', // Limit to posts from the last week
                        'inclusive' => true, // Include the current day
                    ),
                ),
            ));
            
            $i = 1; // Initialize counter
            while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>

            <div class="trending-block">
                <div class="trending-number">
                    <h2><?php echo $i; ?></h2>
                </div>

                <div class="trending-title">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><h2><?php the_title(); ?></h2></a>
                </div>
            </div>

            <?php 
            $i++; // Increment counter
            endwhile; ?>

        </div>
    </section>

    <?php
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

add_shortcode( 'popularesnew', 'popularesnew_func' );

Ten en cuenta que meta_key ha cambiado a 'wpb_post_viewsnew_count' para coincidir con la clave utilizada para almacenar las vistas. Luego mira date_query donde se ha añadido 'after' => '1 week ago' para limitar la consulta a los posts publicados en la última semana.

Victor
300 8 21 16