Title: Custom Posts &#8211; selecting from certain years
Last modified: August 24, 2016

---

# Custom Posts – selecting from certain years

 *  [camweb21](https://wordpress.org/support/users/camweb21/)
 * (@camweb21)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/custom-posts-selecting-from-certain-years/)
 * Hi,
 * I am currently working on a horse trainer website which lists all the horses 
   by their age. e.g. 4 year olds, 3 year olds etc. The horses are a custom post
   called horse.
 * I am currently using this code
 * <?php
    $mypost = array( ‘post_type’ => ‘horse’, ‘date_query’ => array( array(‘
   column’ => ‘post_date_gmt’, ‘before’ => ‘4 years ago’, ‘after’ => ‘5 years ago’)));
   $loop = new WP_Query( $mypost ); ?>
 * Which selects the 4 year olds perfectly from their date of birth. I’ve now found
   out that the horses age isn’t taken from their actual date of birth it’s taken
   from the 1st of January in the year they were born.
 * I’m struggling with the code to detect the current year and then -1 year for 
   1 year olds, -2 years for 2 year olds etc.
 * Any help appreciated. Thank you.

Viewing 1 replies (of 1 total)

 *  Thread Starter [camweb21](https://wordpress.org/support/users/camweb21/)
 * (@camweb21)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/custom-posts-selecting-from-certain-years/#post-6017571)
 * For anyone in the future with this issue I fixed it with the following:
 * <?php
    $mypost = array( ‘post_type’ => ‘horse’, ‘orderby’=>’title’, ‘order’=>’
   ASC’, ‘date_query’ => array( array( ‘column’ => ‘post_date()’, ‘year’ => date(‘
   Y’)-5,+4, ) ) ); $loop = new WP_Query( $mypost ); ?>

Viewing 1 replies (of 1 total)

The topic ‘Custom Posts – selecting from certain years’ is closed to new replies.

## Tags

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

 * 1 reply
 * 1 participant
 * Last reply from: [camweb21](https://wordpress.org/support/users/camweb21/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/custom-posts-selecting-from-certain-years/#post-6017571)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
