Don’t use the direct SQL query, there’s no point and it’s bad practice.
Instead use WP_Query:
$year = date('Y');
$query = new WP_Query(
array(
'year' => $year,
'post_type' => 'news'
)
);
Related Posts:
- Order post by year DESC and month ASC
- Display page content AFTER a loop of posts
- How to start with post number x?
- WordPress custom Query for Posts in Category display posts multiple times
- Dynamically switch template on click
- How to display only one category in a custom post type?
- Trying to exclude custom posts based on date, while sorting by custom field
- How Can I Always Display A Particular Post First Using WP_Query?
- wp query custom orderby not custom field
- Custom Select Query pagination not working properly
- Special Query: Title, Terms, Content – %LIKE%
- Display one post randomly from category on home page
- Need to show birthday of custom post type(Famous people in this case) in elementor. Date of birth set in custom fields
- How to limit the number of posts that WP_Query gets?
- Random sort within an already sorted query
- Setting Custom Sort Order of Posts within a Category
- how to get a different html for odd/even posts?
- Custom permalink structure only for default posts
- Exclude drafts in all() view of edit.php
- How to change the layout and styling of posts according to their category in WordPress?
- WordPress Number of Posts Not Changing With posts_per_page
- adding a custom css class to post
- Prevent duplicate posts in wp_insert_post using custom fields
- Add default content to post (for specific category)
- Pagination not working on Custom Page Template
- display order of connections
- same template for multiple custom post type single
- How to place HTML below the title of the (custom) post overview
- How to get a nofication when post submitted
- .current-menu-item class on custom post type children pages – again?
- Run a function when post is deleted?
- Get posts by multiple ID’s (query)
- How can I create custom button in post.php
- Exclude posts with empty post_content in wp_query
- Only display a certain number of posts, dependent on how many posts there are available in a query
- how to replace embedded “full” sized images within a post with the “large” ones
- Downgrade user from role that allows post creation
- Best way of deletion of old posts [closed]
- Should $found_posts be returned as string in ‘found_posts’ filter hook?
- Animated gif for a thumbs
- I am unable to publish/update post
- Sticky post appears twice
- Get posts of ONE taxonomy term of custom post type
- WordPress Custom Fields (Checkboxes with multiple values)
- How Can I Query a Specific Page From a MultiPage paginated Post
- Custom Post Archive Page not working
- Adding buttons to Add New Post and Add New Page
- filter RSS feed in URL
- Editing the default page to show all posts, rather than most recent ones
- Add a custom meta box in the post options that loads some html code in the header
- Static posts page with home.php
- WordPress Multisite – Create Default Post and New Category On New Site Install
- Custom post types related to same custom post type?
- How to get posts and comments amount per hour, per year and per month?
- Format latest/newest post differently
- relating business and products with custom taxonomy?
- Using setup_postdata() with multi-dimensional array
- Check if a custom post type has already been created
- WordPress Query Posts From Category Post on Static Page
- Edit multiple custom post types while saving a new or edited post
- How to allow multiple thumbnail upload for Posts?
- add external project to web site wordpress on production
- Related Posts function not working
- Change default post style to columns
- Custom Post Type, post-formats associated to custom post type > 404 archives
- Export all posts to import as product items
- Organization of time insensitive content
- WordPress, custom post type and posts query help!
- What would causes search to return incorrect results?
- WordPress Query – Display 5 posts (same post type), each from a given tag
- get posts, run a custom query, and join the results
- Some custom post types did not return an object
- Ignore posts content in the posts queries for internal linking
- Tag for custom post type
- Show post count in custom taxonomy page
- Post relationships/nesting
- Manual Scheduler For Custom Post Type
- Dev and prod on the same database- dev version active for IOS
- How to delete/customize imported demo content from a theme or add my own?
- AJAX load more posts not using correct category and repeating the same few posts
- Displaying dynamic content (blog posts/portfolio) on a static homepage and keep the blog post styles
- single- and archive- templates not being applied to custom post types, not even after flushing permalinks
- Sort Events by Venue Title – Sort Post set by related post ids
- How can you control what a user is allowed to post in the backend?
- How to show content of specific item on the same page?
- How to list all names and descriptions of a custom taxonomy
- How can I have the posts from one word press blog show on the presentation page of another word press site
- Sort post by category using HTML Select tag
- Show a Category Specific Info Box
- Make a custom URL and link to custom Template
- First Custom Post Custom Fields Empty After New Custom Post
- WordPress 3.2 – Problem with WYSIWYG editors in a custom post type?
- How to show only the date, the title and a little “summary” of my WordPress post in my custom theme?
- How to set value in Query Loop?
- Wp_query WooCommerce products
- Only show posts belonging to multiple cateogries at same time
- Dynamic Menu Item
- Display posts with id equal to relationship value
- Sort Posts Alphabetically Based on Specific Category (Divi)
- I have a website issue I am trying to resolve