Jump to content

Question

Posted

Hi guys,

 

This is a bit beyond my skill level. In WordPress I want to output some data from multiple pages. I have a query set up to do this no problem. However, as the query loops through each page, I am adding in some extra data, a points system. Is there a way that I can load all of this data into an array (instead of printing it directly), then sort the array by points, then print the array?

 

Here is my code so far. At the moment it is just printing the needed data, not sorting anything.

					<ol>
					<?php $this_page_id=$wp_query->post->ID; ?>
					<?php query_posts(array('post_parent' => $this_page_id, 'post_type' => 'page')); while (have_posts()) { the_post(); ?>
						<li><?php 
						
						the_post_thumbnail();  
						the_title();
						
						/* Calculate Points. YouTube views multiplied by likes. Relevant video ID is stored in post meta. */
						$video_ID = get_post_meta( get_the_ID(), 'sweetworld_video', true );
						$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
						$JSON_Data = json_decode($JSON);
						$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
						$likes = $JSON_Data->{'entry'}->{'yt$rating'}->{'numLikes'};
						$points = $views * $likes;
						echo $points;
						
						
						?></li>
					<?php } ?>
					</ol>

Help is greatly appreciated.

 

Thanks,

Jordan

Share this post


Link to post
Share on other sites

2 answers to this question

  • 0

Posted

See get_posts, which returns an array of posts. You could loop through this with a foreach, assign the score as another value, then use any of PHP's sorting functions to sort by score.

1 person likes this

Share this post


Link to post
Share on other sites
  • 0

Posted

Thanks threetonesun. That seems to make more sense. Of course I'll need to use get_pages, not get_posts.

 

My problem now is how do I add another value to this? I tried simply:

$this_page_id=$wp_query->post->ID;
$entries = get_pages(array('child_of' => $this_page_id, 'post_type' => 'page'));

foreach ( $entries as $entry ) :
	
	/* Calculate Points. YouTube views multiplied by likes. Relevant video ID is stored in post meta. */
	$video_ID = get_post_meta( get_the_ID(), 'sweetworld_video', true );
	$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
	$JSON_Data = json_decode($JSON);
	$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
	$likes = $JSON_Data->{'entry'}->{'yt$rating'}->{'numLikes'};
	$points = $views * $likes;

	$entry = array('points' => $points);

endforeach;

But that doesn't work.

 

EDIT: Nevermind I got it now. I just loaded what I needed into a new array and then worked from there.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.