Created
May 4, 2019 05:28
-
-
Save rhcarlosweb/1e2908d626c157f6ecdeec407fdc4482 to your computer and use it in GitHub Desktop.
WordPress Popular Posts with WP_Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( class_exists( 'WPP_Query' ) ) { | |
$data_most_viewed = new WPP_Query( [ | |
'post_type' => 'post', | |
'range' => 'last7days', | |
'limit' => 4 | |
] ); | |
$ids_most_viewed = []; | |
foreach ( $data_most_viewed->get_posts() as $id_post ) : | |
$ids_most_viewed[] = $id_post->id; | |
endforeach; | |
$args_posts_viewed = [ | |
'post_type' => 'post', | |
'showposts' => 5, | |
'post__in' => $ids_most_viewed, | |
'orderby' => 'post__in', | |
]; | |
$query_most_viewed = new WP_Query( $args_posts_viewed ); | |
if ( $query_most_viewed->have_posts() ) : ?> | |
<?php while ( $query_most_viewed->have_posts() ) : $query_most_viewed->the_post(); ?> | |
<h4> | |
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> | |
<?php the_title(); ?> | |
</a> | |
</h4> | |
<?php endwhile; ?> | |
<?php endif; | |
wp_reset_postdata(); | |
} else { | |
echo 'Necessário instalar o plugin WordPress Popular Posts para funcionar'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment