Title: Basic PHP variable problem &#8211; custom fields
Last modified: August 20, 2016

---

# Basic PHP variable problem – custom fields

 *  [rjpinney](https://wordpress.org/support/users/rjpinney/)
 * (@rjpinney)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/basic-php-variable-problem-custom-fields/)
 * I’m trying to separate a list of events (a custom post type) into two sections–
   upcoming and passed. I figured the best way to do this was to run two separate
   loops, one under each heading, which compare the custom field date value with
   the current date, one showing things before the current date, the other after
   the current date.
 * I’ve tried various bits of code to set the variable, but can’t get it to work(
   I’ve not used variables in PHP before). Could someone point me in the right direction?
 * Thanks

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

 *  [threestyler](https://wordpress.org/support/users/threestyler/)
 * (@threestyler)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/basic-php-variable-problem-custom-fields/#post-2719381)
 * What do you have so far?
 *  Thread Starter [rjpinney](https://wordpress.org/support/users/rjpinney/)
 * (@rjpinney)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/basic-php-variable-problem-custom-fields/#post-2719466)
 * Thanks for your reply!
 * This is the loop code I have so far, which adds a prefix to each entry depending
   on its category:
 *     ```
       <?php query_posts('post_type=event&meta_key=date&orderby=meta_value&order=ASC'); ?>
       <?php while (have_posts()) : the_post(); ?>
   
       	<li style="list-style:none">
                > 	<?php if ( get_post_meta($post->ID, 'book_launch', true) ) : ?>Book Launch: <?php endif; ?>
       		<?php if ( get_post_meta($post->ID, 'book_signing', true) ) : ?>Book Signing: <?php endif; ?>
       		<?php if ( get_post_meta($post->ID, 'media_appearance', true) ) : ?>Media Appearance: <?php endif; ?>
       		<?php if ( get_post_meta($post->ID, 'public_lecture', true) ) : ?>Public Lecture: <?php endif; ?>
   
       <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>  /  <?php echo get_post_meta($post->ID, 'date', true); ?>
       	</li>
   
       <?php endwhile; ?>
       ```
   
 * The custom field for the date is called ‘date’ (the loop is currently ordered
   by that) and its output format is currently yy mm dd, for sorting reasons, though
   this can be changed.
 * Thanks
 * Rob
 *  Thread Starter [rjpinney](https://wordpress.org/support/users/rjpinney/)
 * (@rjpinney)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/basic-php-variable-problem-custom-fields/#post-2719483)
 * Scrap that last – this is the code I now have. It pulls the posts but the variable
   isn’t working. I planned to run this loop twice, once for events before ‘now’
   and one for events after ‘now.’
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   The maximum number of lines of code that you can post in these forums is **ten
   lines**. Please use the [pastebin](http://wordpress.pastebin.com/)]_

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

The topic ‘Basic PHP variable problem – custom fields’ is closed to new replies.

 * 3 replies
 * 2 participants
 * Last reply from: [rjpinney](https://wordpress.org/support/users/rjpinney/)
 * Last activity: [14 years, 3 months ago](https://wordpress.org/support/topic/basic-php-variable-problem-custom-fields/#post-2719483)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
