0

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.

0

https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

<ul>
<?php 
$item=0;  

$attr = array( 
  'post_type' => 'lyrics', 
  'date_query' => array( array( 'after' => '1 month ago' ) ), 
  'post_status'=> 'publish', 
  'meta_key'       => 'youtube_views', 
  'orderby'        => 'meta_value',
  'order'          => 'DESC',
  'posts_per_page' => 10 
);

$loop = new WP_Query( $attr );
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(); ?>
</ul>
  • Hi Salem, thank you for your answer, however code-only answers are discouraged because they don't explain or teach the OP (or others coming across this) how it works. Could you please edit your answer to explain what your code does? Thank you for your contribution to WPSE. – Tim Malone Jul 6 '16 at 0:39
  • Hi @salem, this didn't work. I guess it's because the youtube_views meta key has a value of 0 for all posts. My goal is to pass $Yviews into it. I read somewhere that I can use update_post_meta(get_the_ID(), 'youtube_views', $Yviews);. I have used it immediately after the $Yviews = 1234; line, but it didn't work. – Stanley Umeanozie Jul 6 '16 at 19:18
  • Is it possible to simply orderby $Yviews? – Stanley Umeanozie Jul 6 '16 at 19:19
0

To order posts by meta fields you will need something like :

$args = array(
    "meta_query" => array(
        array(
            "key" => "youtube-views",
        ),
    ),
    "orderby" => "meta_value"
);

Where you should use the name of the database column as key, usually it should be specified in key - value pairs, but you should be able to use it with key only, however you will need to test it.

  • Hi @knif3r. I know this. Perhaps you misunderstood me? I generated youtube view counts via the API and would like to order posts with the result (views) which is stored in a variable $Yviews. The youtube_views key isn't really relevant to the question because it's empty. I only tried passing the value of the $Yviews variable into it which I do not know how to do properly. Thanks for your response. – Stanley Umeanozie Jul 8 '16 at 18:35
  • In this case refer to the Youtube API or check this plugin : wordpress.org/plugins/wp2yt-uploader – knif3r Jul 10 '16 at 11:28
  • Please stop. You clearly don't understand the question. Thank you for your time. – Stanley Umeanozie Jul 11 '16 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.