Jump to content

Custom arbitrary sorting an array


Chatterton

Recommended Posts

I'm having troubles figuring out some code in a custom WordPress application. It's a book review site, and on the homepage we have 2 columns where we list our latest fiction & non-fiction reviews. Here's a link to give you a better idea:

 

http://www.thelitreview.com/

 

Here's some of the code from the function that makes that happen:

 

$fictionCount = 7; // number of boks to display
$fictionList = array(1471, 1468, 1466, 1477, 1478, 1473, 1427); // these are WP post IDs

$args = array(
	 'cat' => 1,
	 'posts_per_page' => $fictionCount,
	 'post__in' => $fictionList
	 );


		$my_query1 = new WP_Query($args);

			while ($my_query1->have_posts()) : $my_query1->the_post();
			$do_not_duplicate1 = $post->ID;

[do stuff that displays the list]

 

The problem is that the array is automatically sorting itself numerically so that newer posts appear at the top, but since we're constantly adding in new content to older posts we'd like to be able to sort arbitrarily to display the array in the order specified.

 

So how would you propose I go about fixing that up? Do I have to figure out how to assign the values to keys & then sort by the keys?

Link to comment
https://forums.phpfreaks.com/topic/194954-custom-arbitrary-sorting-an-array/
Share on other sites

Try using &get_posts() then look at $posts and figure out how to sort it yourself.

http://codex.wordpress.org/Function_Reference/WP_Query

Also look at

http://postmashfiltered.wordpress.com/2009/06/19/post-mash-filtered/

 

 

HTH

Teamatomic

Thanks, teamatomic.

 

I ended up modifying my args array as follows.

 

$args = array(
	 	'cat' => 1,
		'orderby'=> 'modified',
		'order' => 'DESC',
		'post_type' => 'post',
		'post_status' => 'publish',
		'posts_per_page' => 7,
		'caller_get_posts'=> 1
	 );

 

It sorts the posts into the ones most recently modified and spits out exactly what I need, and now I don't need to manually update which posts to include anymore.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.