• 0

Sorting an Array in PHP (WordPress)


Question

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

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

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.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.