Jump to content



Photo

Sorting an Array in PHP (WordPress)


  • Please log in to reply
2 replies to this topic

#1 jordan.

jordan.

    Neowinian

  • Joined: 14-September 04
  • Location: Antigua, West Indies
  • OS: Windows 7

Posted 19 August 2013 - 13:58

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




#2 threetonesun

threetonesun

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 26-February 02

Posted 19 August 2013 - 14:08

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.



#3 OP jordan.

jordan.

    Neowinian

  • Joined: 14-September 04
  • Location: Antigua, West Indies
  • OS: Windows 7

Posted 19 August 2013 - 15:37

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.





Click here to login or here to register to remove this ad, it's free!