Forked from digisavvy/Bricks Query Loop Posts Ordered by Post Views Counter.php
Created
February 8, 2024 19:26
-
-
Save rabinkumarpal/e08888ba6c60d3c54a63a81e5c7fc5d6 to your computer and use it in GitHub Desktop.
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
// Start of Selection | |
// Initialize the arguments for the WP query to fetch various post types | |
$args = array( | |
'post_type' => ['post', 'session', 'resource', 'video'], // Define post types to query | |
'posts_per_page' => -1, // Set to '-1' to fetch all posts. Replace with a specific number if needed | |
'order' => 'DESC', // Order by descending to get the most recent or viewed first | |
// You can add more arguments here as per your query requirements | |
); | |
// Retrieve the most viewed posts using a function provided by the Post Views Counter plugin | |
$most_viewed_posts = pvc_get_most_viewed_posts($args); | |
// Map the retrieved posts to an array of their IDs for further use | |
$post_ids = array_map(function($post) { | |
return $post->ID; // Return the ID of each post | |
}, $most_viewed_posts); | |
// Construct the arguments for the Bricks query loop | |
// This will ensure that only the most viewed posts are included in the loop | |
// and that they are ordered by the number of views they have received | |
return array( | |
'post_type' => ['post', 'session', 'resource', 'video'], // Specify the same post types as above | |
'posts_per_page' => 9, // Limit the number of posts to display. Adjust as needed | |
'post__in' => $post_ids, // Include only the posts that are most viewed | |
'orderby' => 'post__in', // Maintain the order based on the most viewed posts | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[Sridhar Katakam]
posts_per_page: You may want to set this to a large number that typically exceeds the total number of all the matching posts of your query rather than -1.
$post_ids: Rather than array mapping, you could simply wp_list_pluck() $most_viewed_posts.
so basically something like: 'post__in' => wp_list_pluck( $most_viewed_posts, 'ID' ),