I have a wordpress website where I post song lyrics. I have written a wp query to list specific posts and order them by post views. However, I would like to order them by youtube views instead.
I have been able to get the youtube view counts for the posts (songs), the problem is ordering the posts with it. This is what the code looks like:-
<ul> <?php $item=0; $loop = new WP_Query( array( 'post_type' => 'lyrics', 'date_query' => array( array( 'after' => '1 month ago' ) ), 'post_status'=> 'publish', 'meta_key' => 'post_view_count', 'orderby' => 'meta_value', 'posts_per_page' => 10 ) ); while ( $loop->have_posts() ) : $loop->the_post(); $song_name = get_the_title(); /*YOUTUBE QUERY HERE*/ $Yviews = 1234; <li> <?php echo esc_attr($song_name).' ('.$Yviews.' views)'; ?> </li> <?php endwhile; wp_reset_query(); ?>
Things I Have Tried
I created a custom field and tried updating the value (after the youtube code) with
update_post_meta(get_the_ID(), 'youtube_views', $Yviews); and changed
'meta_key' => 'post_view_count' to
'meta_key' => 'youtube_views' but this either didn't work, or I wasn't doing it properly.
How do I achieve this?
PS: Ultimately, what I am trying to orderby is the sum of the post views and youtube views, but I can live without this.
Thanks in advance.
PS: I have omitted the youtube api query to make the code as simple as possible.