Articles of pre get posts

Restrict Search Query To After Specific Date

This code is not working add_filter( ‘pre_get_posts’, ‘search_filter’ ); function search_filter($query) { if ( ! is_admin() && $query->is_main_query() ) { if ($query->is_search) { $query->set( array( ‘date_query’ => array( array( ‘after’ => ‘January 1st, 2013’, ‘inclusive’ => true, ) ) ) ); } } } I want to remove all results prior to jan 1 2013 […]

How to pass custom parameter to WP_Query for filtering in pre_get_posts

I have an ajax request that returns the result of a WP_Query loop. I would like to detect in pre_get_posts filter the specific wp_query instance that is used in the ajax handler. I thought placing a custom parameter in the $args of the WP_Query in the ajax handler: $args= array(…, ‘ajax’ => ‘AJAX’,//custom param for […]

How to sort custom post type posts in default order by multiple fields?

I’m with a non-profit that paid for development of a custom post type plugin that will help us track endangered plants that we’re keeping in cultivation, rescued from development. We’re mostly happy with it, but need a bit of help. Our plugin is called nasc_report and we have multiple “authors” who are generating reports. The […]

Using meta_query and custom fields within pre_get_posts to return posts within a numerical range

I’m trying to build a search page for music tracks that lets visitors: Choose the post type Choose a genre Choose a mood Specify a Beats Per Minute (bpm) range wp_dropdown_categories() is working for the genres and moods it seems (i.e. I don’t need to modify pre_get_posts), but I can’t limit posts to only those […]

Jetpack Infinite scroll conflicting with theme's pre_get_posts custom posts_per_page

I have the current function on my theme using pre_get_posts, currently right at the bottom of my theme’s functions.php file: function mytheme_portfolio_archive_pages( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( is_post_type_archive( ‘mytheme_portfolio’ ) ) { // Display 20 posts for a custom post type called ‘mytheme_portfolio’ $query->set( ‘posts_per_page’, 20 ); […]

Date Query to Pull Current and Future Posts

I’m trying to use date_query to query posts with todays date or a future date. Think events, I don’t want to show any posts which has a date before todays date. I’m using pre_get_posts and setting the query like so: $query->set( ‘post_status’, array( ‘publish’, ‘future’ ) ); $query->set( ‘date_query’, array( array( ‘year’ => date( ‘Y’ […]

pre_get_posts and the blog page

I am trying to set up my new theme in a proper way. I have created a custom home page for my new theme called “template-home.php” and then I created a “blog” page. I went to settings and changed my “Reading” settings to “A static page” and I set my front page to my home […]

Issue with pre_get_posts on custom post type archive pages

Overview I am creating a theme which has 2 custom post types (as custom plugins): Articles Reviews I have also created single-articles.php and archive-articles.php templates inside the plugin. Same for Reviews CPT. BOTH the custom post types use the default WordPress Taxonomies (categories and tags) My Reading Settings are set to “A Static Page” Front […]

Sorting and limitation with pre_get_posts

This is my third question regarding WP queries and pre_get_posts (sorry about that) but I cant give up one step before the full success 🙂 So the story – my query is limiting front page posts to only those which have featured images. So far so good. My last goal is to sort those posts […]

pre_get_posts: using tax_query only for certain post type

I have default post type posts (post type: post) and custom post type posts (post type: cpt). The latter has a taxonomy (cpt_tag). I would like to display on my front page all the default post type posts (unfiltered), and some of the cpt post type posts, only if they have a certain cpt_tag. The […]