I recommend to hook into the pre_get_posts() function to change the query by adding this code to your functions.php file:
<?php
function filter_by_date( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( isset( $_GET['year'] ) ) {
$query->set( 'year', $_GET['year'] );
$query->set( 'post_type', 'events' );
}
}
return $query;
}
add_action( 'pre_get_posts', 'filter_by_date' );
?>
Related Posts:
- Stop WordPress Wrapping Images In A “P” Tag
- Is there an if statement that can determine if a post in the loop is the last post?
- Access post from post id in URL
- WP_Query: Why is sticky post not first item in loop?
- How to mark articles as read?
- Ajax Load More Posts in Category Page
- Is there a way to disable the sticky posts feature?
- How to remove Nextpage tag inside posts text depending of utm_campaign
- using slug instead of ID in admin edit post url
- Can I load posts via Ajax?
- WooCommerce – Create Products Programatically [closed]
- Code to auto expire posts after 30 days
- Search not working with title and content
- Show the title of the latest post by author
- Developing a secure front end post editing form
- Filter posts by author and category simultaneously
- WordPress Delete hook with wp_delete_post function?
- How to use alternate post layout at key points in post loop?
- Add a custom option to a page in backend
- Changing publish date does not update post order
- Hide Post comments when displayed via WP_Query
- Show all post titles
- Posts are not showing up on particular category
- Can a page_id and a post_id be same?
- Differentiate between posts and pages in search results
- WordPress Theory Lesson – Image Columns within posts with alignment?
- Uppercase first sentence in every post
- How to Orderby Comments by post title?
- If modified on same day, show only time
- How is a gallery associated with some post in database?
- Page Navigation for list of post
- Extracting post categories
- Limit popular posts by days
- Exclude post_meta from Rest API Endpoint
- impreza portfolio display/view blank page
- How to verify wp user password by sql query in wp? [closed]
- Get ALL post ID’s export list (Only id’s. Php or sql not important )
- Automatically add custom fields value to wordpress post
- How to use format post in a pertinent way
- Meta value does not save for scheduled posts
- How can I check if a var is equal a translated label?
- Loading post with Ajax
- How to create media library for each user?
- How to display links in specific page
- WordPress does not output “post” class when calling post_class() function
- When creating a post how do you select the format?
- How can I post exact same post but with a few things changed? (with some sort of Automation)
- Loop doesn’t show title of second post and posts thereafter
- Delete all drafts?
- Which PHP files should I edit for changing POSTS list and view pages?
- How to add add more properties to WP_Post object in search results loop
- How to get current post ID in functions.php
- How to show all posts by author on buddypress profile with navigation
- Get URL from all images in a post
- Change the default blog post post attribute template name from “default template” to something else
- Display post by click on the link
- WordPress 302 Redirect to Random Product/Post/Page
- How to call posts under a specific category on static front page?
- Image on rss feed on post
- Using AJAX and PHP to load next post object
- Limiting post list by taxonomy
- select post preview image from nextgen gallery
- WP_Query articles order by offset in collumns
- sort posts by getPostViews in functions.php
- Making a wordpress page print friendly
- Different post views for different category views
- “Home” Page now only displaying the single latest post
- Possible?! A contextually titled back link
- Post navigation using date
- Error while excluding a post from another loop (using its ID)
- How to select a category automatically based on a word in WordPress post title?
- Post feature image after post title for single post
- How to get unmodified post content?
- I have inserted a post’s content in homepage.php by the post-id. The font isn’t matching with the theme. How do I change font of this content?
- Random post_id at wp_insert_post
- How to retrieve all meta data directly from the $post object? Such as $post->related_topics?
- How to use two query result together in one loop
- Odd hyphen added at end of blog post
- load post data into mysql
- filter posts based on menu_order
- Get a list of categories that are related to posts
- How can change header text in post careate/edit form
- Difference between modified post and new post
- All my posts don’t show, only the headline is displayed on the homepage
- How to prevent people from seeing an attachment to a scheduled post that’s released in future?
- How can I access my posts on my wordpress site, now I’ve lost the domain name for it?
- Page not loading content – Body archive – page
- How Can I Limit Users to Post Creation For My Frontend Theme?
- how to create add-able metaboxes in wordpress post edit screen?
- Daily posts like an archive
- Retrieve latest post by multiple categories with ID
- standard wpGallery for post won’t work
- Why is $_POST Empty in Profile Edit Admin?
- How remove “recent comments” title without modifying code? [closed]
- Return All Post from publish to draft [duplicate]
- Show Title/Date/Excerpt of first post & only Title for rest in Query [duplicate]
- remove description in colum title post
- Get posts after calculating meta key
- Articles show some of text [duplicate]
- Output number of WordPress posts remaining until a post milestone is reached