Title: A loop based on meta data
Last modified: August 20, 2016

---

# A loop based on meta data

 *  [jeffeggleston](https://wordpress.org/support/users/jeffeggleston/)
 * (@jeffeggleston)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/a-loop-based-on-meta-data/)
 * Below is a code I’m using to create a second loop showing a series of posts for
   movies and plays and whatnot. I am a PHP novice. I have added all of the data
   on the post pages that is required and yet I get only one post showing up in 
   the loop feed. Sometimes nothing shows up if I change the category (from movies
   to featured-event). Here’s the URL:
 * [http://library.jeffmeggleston.com/](http://library.jeffmeggleston.com/)
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_

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

 *  Thread Starter [jeffeggleston](https://wordpress.org/support/users/jeffeggleston/)
 * (@jeffeggleston)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/a-loop-based-on-meta-data/#post-2556145)
 * This code is based on the following page on WP Recipes:
    [http://www.wprecipes.com/how-to-set-post-expiration-datetime-on-your-wordpress-blog](http://www.wprecipes.com/how-to-set-post-expiration-datetime-on-your-wordpress-blog)
   I’ve gotten this code to work on the “Movies” page of the site using the following
   version of the code:
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 * Thank you in advance for your help!!!!!
 *  Thread Starter [jeffeggleston](https://wordpress.org/support/users/jeffeggleston/)
 * (@jeffeggleston)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/a-loop-based-on-meta-data/#post-2556168)
 * I should also note that depending on how I edit the code above (the first one)
   it occasionally erases the meta data in my database on one or many of the posts.
   That is irritating.
 *  Thread Starter [jeffeggleston](https://wordpress.org/support/users/jeffeggleston/)
 * (@jeffeggleston)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/a-loop-based-on-meta-data/#post-2556175)
 * This is the code that is not working on the homepage.[http://pastebin.com/eU4Rcwr1](http://pastebin.com/eU4Rcwr1)
 * This is the code that works on my “movies” page.
    [http://pastebin.com/ATwFigig](http://pastebin.com/ATwFigig)
 *  [AngelEyes](https://wordpress.org/support/users/clustervision/)
 * (@clustervision)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/a-loop-based-on-meta-data/#post-2556238)
 * not sure if you’ve seen this, but custom field queries are built into WP_Query
   and are relatively straight forward. I just used this type of query on a recent
   website to find a couple “related posts” by posts with the same “author” meta
   key /value.
 * A note:
    the only way I was able to get the query to work was the more extended
   version that is used for multiple key pairs, even though I was only using one
   key pair
 *     ```
       $args = array(
       	'post_type' => 'product',
       	'meta_query' => array(
       		array(
       			'key' => 'color',
       			'value' => 'blue',
       			'compare' => 'NOT LIKE'
       		),
       		array(
       			'key' => 'price',
       			'value' => array( 20, 100 ),
       			'type' => 'numeric',
       			'compare' => 'BETWEEN'
       		)
       	)
        );
       $query = new WP_Query( $args );
       ```
   
 * [http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters)

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

The topic ‘A loop based on meta data’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 4 replies
 * 2 participants
 * Last reply from: [AngelEyes](https://wordpress.org/support/users/clustervision/)
 * Last activity: [14 years, 4 months ago](https://wordpress.org/support/topic/a-loop-based-on-meta-data/#post-2556238)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
