I have defined a post_view_count
meta_key to store the number of post views.
I would like to get an array of post ids sorted by post_view_count
value to use in the
functions.php
file.
Thanks.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityIt's easy. :) You would use get_posts function. See WP_Query class for complete list of parameters that you can use.
function get_posts_by_view_count() {
$ids = array();
$args = array(
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'post_view_count'
);
$posts = get_posts( $args );
if( $posts ) {
foreach( $posts as $post ) {
$ids[] = $post->ID;
}
}
return $ids;
}