Title: Order posts by Custom Fields value
Last modified: February 26, 2017

---

# Order posts by Custom Fields value

 *  [erwin_m](https://wordpress.org/support/users/erwin_m/)
 * (@erwin_m)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/order-posts-by-custom-fields-value/)
 * Hi,
 * I’m looking for a special way to order my posts.
    I want all my posts, about 
   1500, to be ordered at random but in two sections, so first section shows random
   the posts belonging to that section, beneath that I want to show the other section
   in random order.
 * How can I do so? Is the best way to use a Custom Fields value, or is there another
   way? And how do I do that? I’m using Enfold as theme, but this is, off course,
   far beyond their support.
 * Regards,
 * Erwin

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

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 3 months ago](https://wordpress.org/support/topic/order-posts-by-custom-fields-value/#post-8849845)
 * Before you go into coding, note that if you cache your pages, you won’t really
   present your posts randomly…. The first visitor will create the cached version
   and that same page will be presented to subsequent visitors until the cache is
   regenerated.
 * Anyhow, assuming you’re using categories to define what post goes in what sections,
   you’ll just have to write two loops (with paging?), segregating posts by category
   and using a random order.
 * Spend some time here: [https://codex.wordpress.org/Class_Reference/WP_Query](https://codex.wordpress.org/Class_Reference/WP_Query)
 *  Thread Starter [erwin_m](https://wordpress.org/support/users/erwin_m/)
 * (@erwin_m)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/order-posts-by-custom-fields-value/#post-8849955)
 * Hi Steve,
 * Thank you for your reply! Caching won’t be a big problem. If the post are once
   a day are randomized, it’s good enough. So, I will rebuild my cache daily.
 * I’m building a site with advertisements for accommodations in different country’s/
   regions. For that I use the categories. The accommodations are there in two versions,
   some big and some small. I want the bigger ones first and after that the small
   ones. I was thinking of giving them a custom field key and value and using them
   for the sections.
    I’d already discovered the page where your linking to, but
   can’t figuring out how to use this in my case. I was also looking at [this](https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters),
   it looks promising, but I don’t know where to put the code and how it’s gonna
   work with my template. Do I have to change the loop, or can I place this code
   in my functions.php?
    -  This reply was modified 9 years, 3 months ago by [erwin_m](https://wordpress.org/support/users/erwin_m/).
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 3 months ago](https://wordpress.org/support/topic/order-posts-by-custom-fields-value/#post-8849974)
 * I think you’re going to need to build a custom page template to get what you 
   want. You could use custom fields, categories, or tags — whatever is easiest 
   for you.
 *  Thread Starter [erwin_m](https://wordpress.org/support/users/erwin_m/)
 * (@erwin_m)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/order-posts-by-custom-fields-value/#post-8850322)
 * Tags are also already used for creating a result page ; ). So it has to be the
   custom fields. I’ve created a custom field Advertisements with the values big
   and small. Now I’m trying to implement them.
 * The query should looks like, also thats what I think:
 *     ```
       $args = array(
       	'post_type'  => 'product',
       	'meta_query' => array(
       		array(
       			'key'     => 'Advertisements',
       			'value'   => 'big',
       			'compare' => '=',
       		),
                       array(
       			'key'     => 'Advertisements',
       			'value'   => 'small',
       			'compare' => '=',
       		),
       	),
       );
       $query = new WP_Query( $args );
       ```
   
 * As I track the loop in my theme, I think I should implement it in the next code,
   thing is, where to put it, this is the original file.
 *     ```
       <?php
       global $avia_config, $post_loop_count;
   
       $post_loop_count= 1;
       $post_class 	= "post-entry-".avia_get_the_id();
   
       // check if we got posts to display:
       if (have_posts()) :
   
       	while (have_posts()) : the_post();
       ?>
   
       		<article class='post-entry post-entry-type-page <?php echo $post_class; ?>' <?php avia_markup_helper(array('context' => 'entry')); ?>>
   
       			<div class="entry-content-wrapper clearfix">
                       <?php
                       echo '<header class="entry-content-header">';
                           $thumb = get_the_post_thumbnail(get_the_ID(), $avia_config['size']);
   
                           if($thumb) echo "<div class='page-thumb'>{$thumb}</div>";
                       echo '</header>';
   
                       //display the actual post content
                       echo '<div class="entry-content" '.avia_markup_helper(array('context' => 'entry_content','echo'=>false)).'>';
                           the_content(__('Read more','avia_framework').'<span class="more-link-arrow">  &rarr;</span>');
                       echo '</div>';
   
                       echo '<footer class="entry-footer">';
                       wp_link_pages(array('before' =>'<div class="pagination_split_post">',
                                               'after'  =>'</div>',
                                               'pagelink' => '<span>%</span>'
                                               ));
                       echo '</footer>';
   
                       do_action('ava_after_content', get_the_ID(), 'page');
                       ?>
       			</div>
   
       		</article><!--end post-entry-->
   
       <?php
       	$post_loop_count++;
       	endwhile;
       	else:
       ?>
   
           <article class="entry">
               <header class="entry-content-header">
                   <h1 class='post-title entry-title'><?php _e('Nothing Found', 'avia_framework'); ?></h1>
               </header>
   
               <?php get_template_part('includes/error404'); ?>
   
               <footer class="entry-footer"></footer>
           </article>
   
       <?php
   
       	endif;
       ?>
       ```
   
 * Can anyone tell me where to put it and also check if the first part is okay?
 * Regards,
 * Erwin

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

The topic ‘Order posts by Custom Fields value’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [erwin_m](https://wordpress.org/support/users/erwin_m/)
 * Last activity: [9 years, 3 months ago](https://wordpress.org/support/topic/order-posts-by-custom-fields-value/#post-8850322)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
