I tried your code and is working fine for me
$args = array( 'post_type' => 'project','posts_per_page' => -1,'year' => date('Y') - 2, 'orderby'=> 'title','order' => 'ASC');
$myposts = get_posts( $args );
The thing is as you can see he is only fetching the posts from 2015. If you don’t have any posts with the publish date from 2015 you will get nothing, but if you want to grab all posts from two years ago until the current year you have to do the following:
$args = array(
'post_type' => 'project',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'date_query' => array(
array(
'after' => date('Y')-2 . ' year ago'
)
),
);
See WP_Query for reference
Related Posts:
- Only showing the_date and the_excerpt for first entry in get_posts
- Unique Problem with the php date and get_posts
- Get_posts not returning any posts when used with switch_to_blog
- How do I get posts by multiple post ID’s?
- What does setup_postdata ($post ) do?
- Query for custom post type? [closed]
- setup_postdata() does not seem to be working?
- Regenerate Slugs From Title of Posts
- get_posts – get all posts by author id
- Why get_posts are only showing five posts (retrieved by assigning a category to them?
- Exclude Current Post from Recent Posts Loop
- WordPress get_posts by category
- get_post random and order by not working
- Alternative to get_posts() due to multithreading cache crash
- How to get current post ID in quick edit callback
- get_posts with meta_compare=’LIKE’ not working
- Get Recent Posts by Date in Multisite
- How to restore deleted pages/posts?
- Sort “get_pages” by menu order not ordering
- get posts based on meta value of the author
- date_query not returning some posts in date range
- Get the exact SQL query that get_posts() generated
- How do you get posts by meta_query using the JSON API plugin?
- Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?
- Query posts ordering by title, but ignore ” and special characters
- get_posts inside cron
- get_posts not honoring post_status
- get_posts returning empty array
- get_posts only children from certain parents
- Problem with get_posts, tax_query and counting the number of posts
- get_posts cannot grab from specific category
- Filtering custom post type query
- Check If posts exist in custom post type category, outside of loop
- Get posts by meta value with date
- data returned from get_post($postId) have different keys than wp-json/wp/v2/posts/{postId}
- How to limit get_posts()?
- meta_query date and time comparisons
- get_posts from post x(offset=>x) to end
- With get_posts(), how can I put a category as a variable
- What’s the difference between “get_posts” and “wp_get_recent_posts” when used with “setup_postdata”?
- Serialized array, grab specific posts with meta_key/meta_value[0]->is_featured
- How do I combine these 2 queries
- get_posts gives different result than wpdb->get_results
- Increase the page size of the WordPress REST API
- How to generate a list of posts published on current day?
- Trouble using get_post
- Cache Get_posts
- Get latest posts from multiple categories
- get_posts() returns all posts rather than the ones specified with ‘post_author’ =>
- Query random post from different categories
- Get_Posts, only if in both categories
- Posts not showing in correct Alphabetical or ID order when using get_posts / orderby but only on production server
- get_posts() excluding all children of a specific post/page
- Secondary Query Is Breaking Main Query
- If clauses in get_posts query
- var_dump and print_r cause white screen
- `get_posts()` ignore my custom post
- excluding current post from get_posts
- What’s wrong with my date_query?
- How to query posts to include specific pages of one post type, and all pages of another post type?
- Only retrieve posts where post_excerpt has been filled out
- Can I use numberposts=-1 and offset together when using get_posts()?
- Automatically republish old posts
- Ordering posts in get_posts
- How can I display a single post link, based on title sample and change monthly?
- get_posts() doesn’t consider user permissions
- Meta query: get posts with value in a multidimensional array
- Get posts inside Get terms problem
- Better way to list links with different meta values using same meta key?
- Ho to add get_relative_date and post_except to this get_latest_post query?
- How to update all posts but the current one (post__not_in not working?)
- How to get the post content from a category on my homepage
- get_posts() not working in functions.php
- Query Problem in getting top viewed posts
- get_posts not working as expected
- Get all pages and posts with get_pages() or get_posts()
- Getting only a specific post type with get_post?
- Display post details by post ID
- What is the p parameter in a get_posts() parameter array?
- Exclude post format from get_posts
- Memory issue with get_posts( ) function
- get_posts() using an array of post ids [closed]
- Iterate over get_post_meta() result
- How can I show the author’s latest post with title?
- Inserting post content into pages when using custom taxonomies
- get the post_date of ID
- Get posts by language in Polylang plugin
- Query get_posts by level depth
- Get posts in between specific ID’s
- Sort Posts By Custom Field Date?
- Auto-change Post Status on First Page Load
- How to use WordPress Conditional Tags in get_posts foreach loop
- Don’t submit post if post title exists
- Hide gravity forms
- Why would get_posts be ignoring posts with 2-digit ids?
- Not able to get random post
- Order custom post type by custom field (created by PODS) via PHP
- Local Post Date in WordPress Admin Panel Tables
- get_posts returns all posts
- Override Blog pages show at most with get_posts