Title: sorting $post?
Last modified: August 18, 2016

---

# sorting $post?

 *  [AndreSC](https://wordpress.org/support/users/andresc/)
 * (@andresc)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/sorting-post/)
 * I built a loop around The Loop to group category listings according to a custom
   field value, but because each loop needs to run through all the posts, to select
   and show its relevant posts, I had to disable the limit of how many entries are
   posted – problem :- (
 * Can one (and how) sort the content of the $posts variable (e.g. php?) before 
   calling The Loop, any pointers?
 * e.g. [http://www.davidkrutpublishing.com/dkp](http://www.davidkrutpublishing.com/dkp)
 * thanx

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [AndreSC](https://wordpress.org/support/users/andresc/)
 * (@andresc)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/sorting-post/#post-346270)
 * e.g. my code is
    `<!-- WP Loop starts here --> <?php if (have_posts()) : ?> <?
   php // loop through the 3 grouping critera $i = 1; while ($i <= 3): ?> <?php 
   while (have_posts()) : the_post(); ?> <?php $pub=get_post_meta($post->ID, 'Publisher',
   true); $kind="Books";// by default $pubkey=""; if ( in_category(13) ) { $pub="
   David Krut Publishing "; $kind="News"; // to allow check to for comments or not
   later }
 *  if ($i == 1 ) {
    $pubkey="David Krut Publishing "; }  if ($i == 2 ) { $pubkey
   =" Tate "; }  if ($i == 3 ) { $pubkey=" Other"; }  // print only selected group
   for this run if ($pub==$pubkey){?>… `<?php if ($kind=="News") { the_excerpt();
   echo "<p><a href=\""; the_permalink(); echo "\" rel=\"bookmark\" title=\"Link
   to"; the_title(); echo "\">"; echo "View this entry in full &raquo</a></p>"; }
   else { the_content('Read more &raquo;'); } ?> <?php if ($kind=="Books") { include(
   TEMPLATEPATH . '/print-fields.php');// Print custom fields  include (TEMPLATEPATH.'/
   order_this.php'); } ?> </div>…
 *  [spencerp](https://wordpress.org/support/users/spencerp/)
 * (@spencerp)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/sorting-post/#post-346271)
 * I wouldn’t post that though AndreSC, a moderator might delete it for too long
   of code.. paste it in pastebin.com and give that url..
 * spencerp
 *  Thread Starter [AndreSC](https://wordpress.org/support/users/andresc/)
 * (@andresc)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/sorting-post/#post-346372)
 * okay – I forgot about that, sorry forum

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘sorting $post?’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [sorting](https://wordpress.org/support/topic-tag/sorting/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [AndreSC](https://wordpress.org/support/users/andresc/)
 * Last activity: [20 years, 3 months ago](https://wordpress.org/support/topic/sorting-post/#post-346372)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
