wordpress get a custom post type from the last year

wordpress drive me mad!! things that is regular php is easy, I get stack with.
I created a custom post type, now I need to query and present all the post from this custom post type just from the last year.
I have this:
$args = array(
	'post_type' => 'rs_event',
	'date_query' => array(
		array(
			'column' => 'post_date_gmt',
			'before' => '1 year ago',
		)
	),
	'posts_per_page' => -1,
);
$the_query = new WP_Query( $args );

Open in new window

when I do var_dump I don't get the posts.

what am I missing here?

best regards
LVL 1
derridaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason C. LevineDon't talk to me.Commented:
Your code doesn't contain a query, just arguments.  The easiest way to get posts is via a Loop construct:

https://codex.wordpress.org/The_Loop

Check the samples there and compare them against your own code...
derridaAuthor Commented:
hi
first, thanks for addressing my question. but i hope you understand that i have read the codex but still miss something. this is why i posted the question.

after the query i tried this, but i don't get any results:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php echo the_title(); ?>
<?php endwhile; else : ?>



<?php endif;
wp_reset_postdata(); ?>

Open in new window


maybe because it is a custop post type?  

by the way i also tried:
$args = array( 
  'numberposts' => -1, 
  'order'=> 'DESC', 
  'orderby' => 'post_date', 
  'post_type' => 'rs_event' 
  );
  $theevents = get_posts( $args );

Open in new window

when i do var_dump i do get all the posts, but i cannot restrict it for the last year. and on this i can just do regular foreach, without using theloop.

i am sure i am not getting something here.
derridaAuthor Commented:
ok. managed to do it with get_posts with limit by date like so:
  'date_query' => array(
        'after' => date('Y-m-d', strtotime('-1 years')) 
    ) 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Jason C. LevineDon't talk to me.Commented:
Okay.  Understand that the only thing I know about you is what you post.  If you post a code snippet that looks incomplete, I'm assuming you may not know the full syntax.  Thank you for telling me what else you tried and providing more code.

Try this.

$args = array(
	'post_type'      => 'rs_event',
	'post_status'    => 'publish',
	'orderby'        => 'date',
	'order'          => 'DESC',
	'date_query'     => array(
                            	array(
                            		'before' => '1 year ago'
                            	)
                        )
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) {
  
	while ( $my_query->have_posts() ) {
		$my_query->the_post();
		
		echo get_the_title() . '<br />';
		
	}
	
}
wp_reset_postdata();

Open in new window

derridaAuthor Commented:
hey Jason. just now seen you also suggest  the date_query. but the bizzare thing is when i try what you suggested with WP_Query, it still doesn't give me any results, while what i did with get_posts do give me the right results.
derridaAuthor Commented:
managed to fix it. and posting in case others will find it useful
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.