Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [pre-get-posts]

Refers to a hook in WordPress Core that can be used to alter the results returned by a WP_Query based database query.

30
votes
11answers
22k views

Sort on meta value but include posts that don't have one

I've been modifying the built in WP search using the pre_get_posts filter, allowing the user to sort the posts (including a bunch of custom post types) by different fields. The problem I'm having ...
1
vote
1answer
640 views

Only show certain post types in recent posts widget

I have a custom post type which has been made to show in archives using the code below in my theme's functions.php : add_filter('pre_get_posts', 'query_post_type'); function query_post_type($query) { ...
-1
votes
0answers
15 views

Showing all posts in specific term except the first 10

My client wanted to display all posts except the first 10 in one of the term in a custom post. Additionally, the pagination between the pages should work correctly. I know I need to use pre_get_posts, ...
2
votes
2answers
126 views

Meta query interfering with orderby relevance

I'm trying to re-order the search results using the pre_get_posts hook and set the orderby parameter to relevance. I also want to put priority posts, which is based on post meta _prioritize_s to be ...
0
votes
0answers
22 views

Display Specific Posts at Start of Loop

I'm attempting to mimic 'sticky' functionality on a custom post type using a simple ACF true/false field. I'd like to use pre_get_posts to modify my query to first show my 'sticky' posts at the top ...
2
votes
1answer
936 views

How do I sort posts by custom taxonomy?

I am trying to sort posts by custom taxonomy. I have a custom post type which is course-cpt and I have created custom taxonomies within it, I need to sort the courses by course-level (which is the ...
1
vote
0answers
22 views

Blog page pagination is not working after using the offset argument

I have displayed the recent 7 posts then shown the remaining posts. So I have added the offset = 7 in the argument to skip above 7 recent posts and display remaining posts but the pagination not ...
0
votes
1answer
18 views

$query->is_main_query() is causing query's tax_query to be ignored

I was having issues when modifying the main query hooking onto pre_get_posts as it was causing the menu to not be displayed. This the original code: else if ( isset( $_GET['category'] ) && !...
5
votes
1answer
432 views

Order Search Results Page by meta_value If no Value Return Remaining Results

I am trying to order the search results page of my site by a specific meta_key. The catch is I need to return all results even when there's no value in the meta_key The posts are custom post types ...
0
votes
1answer
33 views

Meta Query “IN” doesn't work with ACF checkbox filter

I try to integrate to my CPT archive page some ACF filters. I start with this tutorial : https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/ It works when I use it ...
0
votes
0answers
25 views

Duplicate posts when using pre_get_posts for author.php

I'm trying to change the main query into the author.php but there are some duplicate posts?! How can I fix it, Thank you! function change_author_query( $query ) { if ( $query->is_author() &...
0
votes
0answers
24 views

How do I search WordPress posts by categories and tags in addition to title and content?

I would like to expand my site's default search fields (title and content) to include categories and tags without installing a plugin. Including the below code in functions.php allows me to ...
1
vote
1answer
30 views

My Main Query Modification is Messing up my dynamic main - why?

This has been very difficult to track and I have enabled only the plugins I need (ACF and my own plugins that deal with content type creation). I noticed that on some pages, my menus were gone, this ...
0
votes
1answer
51 views

Woocommerce - exclude only older out-of-stock items

Trying to find a way to exclude out of stock items from listing, but only after some time (let's say, if they're older than one month). I know I can exclude all of them easily, or change the order ...
0
votes
1answer
425 views

pre_get_posts with WooCommerce Shortcode Query

I want to add some additional query vars and customise the query for woocommerce [products] shortcode. i'm trying to add some additional fields and conditions with pre_get_posts and posts_fields hooks ...
0
votes
1answer
22 views

Pre get posts where template is not equal to one specified?

I'm trying to not show certain posts in the admin area based on the page template. Here is what I have so far: add_filter( 'parse_query', 'hide_some_posts' ); function hide_some_posts( $query ) { ...
1
vote
1answer
45 views

Get .subsubsub count of post per status queried using pre_get_posts

My following query using pre_get_posts shows the Author only certain posts or pages with certain statuses that s/he should only see and of what belongs to him/her. Is there any way I can get a count ...
1
vote
1answer
379 views

Sanitize Custom Field Value in Meta Query

I'm using the pre_get_posts hook with meta_query args to filter an archive loop by dates. Basically, there is a custom post type of "events", and my users type in a date as a custom field for the ...
-1
votes
1answer
12 views

How to pass >= condition filter to my year custom tax_query

I have a custom taxonomy called year. I need to filter posts by pre_get_posts tax_query from year value to to year value. I am searching from one hour now feeling stuck. Please guide me. Thank you
0
votes
0answers
15 views

Change default wp-query using pre_get_posts

I would like to show all the posts of a post format on top of the default wp query and show all the other post formats below them (in the same query) using pre_get_posts. Please note that I need to ...
0
votes
0answers
27 views

Setting a product tag archive page as front page

I'm trying to set a product tag archive page as the front page. Using this code: function set_product_tag_archive_as_front_page( $query ) { // Only filter the main query on the front-end if ...
1
vote
1answer
33 views

Custom post type show on both Home and Date Archive

Found below snippet to pull posts from a custom post type 'pictures' into the home page and date archive page.. Added to functions.php this works for home page: function ...
1
vote
1answer
408 views

Alter query using pre_get_posts() if meta_key is present

I am trying to modify the number of posts per page for archive pages that contain category sticky posts (added by the Category Sticky post plugin) to do so I am using the pre_get_postshook and have ...
0
votes
1answer
43 views

Automatically applying a pre_get_posts filter for child categories only

I have a category named "Research" that has a growing list of child categories. This research category has a special template category-research.php that shows a list of its children instead of the ...
3
votes
1answer
27 views

How to show more posts on an archive page?

I'd like to show more posts on my custom archive page, but can't seem to figure out the right way to do it. Currently it displays 10, which is the number set in WP Settings and the number I'd like on ...
1
vote
1answer
81 views

Does pre_get_posts affect REST API responses?

I'm trying to limit the content that is returned via REST API routes to only contain content published by the current logged in user. I'm trying to do this using pre_get_posts which works fine when in ...
1
vote
1answer
407 views

Filtering out child category posts from parent category archive not working

In this example cat 5 is the parent category, and 42 is a sub-category. I the main category 5 archive I don't want any posts from sub-category 42 displaying. Why isn't this working? function ...
1
vote
1answer
463 views

Pre get posts for single post

I pre get posts for a events archive page: http://www.netzwerk-leipziger-freiheit.de/veranstaltungen/ function my_pre_get_posts( $query ) { // do not modify queries in the admin if( is_admin() ){...
0
votes
1answer
407 views

How to display custom post type?

I created custom post type called book. I'm trying to make a template in my theme so when I use this template for any page in my theme instead of fetching normal post type (I mean my posts) it fetches ...
1
vote
1answer
446 views

pre_get_posts for two loops on same page

I'm using two loops on homepage, one is to show the 3 latest posts, and the other one to show the rest of the posts minus the 3 first posts. I'm using the following code for the pre_get_posts. ...
0
votes
0answers
52 views

How to offset main query without affecting other queries and backend

I'm trying to offset my main blog query by 3 posts whilst trying to get the pagination to function correctly. I've been following the codex reference here but I'm struggling to get it working as I ...
3
votes
2answers
478 views

How to target the default Recent Posts and Recent Comments widgets with pre_get_posts?

I added the following to my functions.php: add_action('pre_get_posts', 'keyl_get_emp_posts'); function keyl_get_emp_posts($query) { if ($query->is_main_query()) $query->set('...
0
votes
0answers
10 views

Display random posts, favoring posts published close to todays date regardless of year

I would like my start page to display random posts, but put posts published close to today’s date first and posts published far from today’s date last - but regardless of what year the posts were ...
0
votes
2answers
557 views

pre_get_posts gives 404 error on Custom Post

I know this has been covered in different ways a thousand times (I read all the posts), but it seems not specifically for my case. I can't really get it to work myself, being a big development noobie. ...
0
votes
1answer
469 views

Hooking get_pages()

On a site this is a plugin that uses get_pages() to make a list of pages. The list is used to create a list of pages and create next page and previous page links. ( http://wordpress.org/plugins/next-...
0
votes
1answer
24 views

Admin table Query filtering “WHERE” for custom post type

So I have been searching for a solution on how I can dynamically change a CPT admin table query by filter the result WHERE Status is equaled to some custom status. I have searched using function ...
0
votes
1answer
562 views

Apply pre_get_posts to specific custom post type in the admin area

I've created a few CPTs, and wish to have one of the custom post types to query from other post types. So what I did is I used the codes below to alter the query: add_filter('pre_get_posts', '...
2
votes
2answers
177 views

RSS feed with specific keyword

I am able to get articles as RSS feed from below url http://wordpress_site/blog/feed I am also able to get articles from BlogSpot with specific keywords as below https://www.yourblogname.blogspot....
0
votes
1answer
518 views

Order posts by custom column using pre_get_posts

Is it possible to sort posts by custom column in wp_posts table (in this case, 'fav_count' column) using pre_get_posts filter? If not, is there any other way to do this before the main query is run, ...
0
votes
1answer
29 views

How to change posts order on Category and Tag archives?

WordPress by default order posts in descending order by date. But I want them to be in ascending order by date. How do I turn the order to ascending only for tags and category? I don't want blog post ...
0
votes
0answers
17 views

Sort posts by tags in a category page

Forgive me in advance for my English. To explain, I would sort posts by tags, by providers and alphabetically. I looked at this post but did not understand the steps. I can not do anything. I already ...
0
votes
0answers
40 views

pre_get_posts meta_query for dates interval

I have an "activity" custom post type, that has start and end dates introduced via custom fields. I want to add a meta_query argument through pre_get_posts, to get "activity" posts between two dates. ...
1
vote
1answer
580 views

I am trying to hide a custom post type category to logged in users with Pre_Get_Posts

I have a custom post type in my theme called portfolio. It's categories are called project-categories. I am trying to exclude a project category for logged in users. I cannot get this to work at all. ...
0
votes
2answers
612 views

pre_get_post filter returns results when there should not be

I am trying to filter a CPT by an array of post ids using pre_get_post filter within my plugin. Everything works fine when there are post that match the ids, but when there are no post that match the ...
0
votes
0answers
51 views

Pagination not working past page 3 on archive page of category

I have a custom post type of "show" and within that custom post type I have set up a taxonomy / category called "venue" which has two options "venue-one" and "venue-two" (slugs). On the two archive ...
1
vote
1answer
26 views

Posts will not display on page-mypage.php

Something is really tripping me up. I have been used to querying posts via custom queries however based on research lately I have learned that for the most part this is a big no no. So, in testing the ...
0
votes
1answer
612 views

using posts_where for meta data on pre_get_posts

Let's say you have some code which filters posts by using pre_get_posts e.g. add_action( 'pre_get_posts', 'be_exclude_category_from_blog' ); function be_exclude_category_from_blog( $query ) { ...
0
votes
1answer
27 views

Pre get posts sort by meta key returns no results if meta key does not exist

I am able to sort my posts by a custom field ('tour_length_days'). However if this field does not exist on an existing post then it will return no results. I was hoping there was a way to ...
0
votes
0answers
26 views

Change post category within pre_get_posts

I have written a filter to be called in pre_get_posts to remove posts from a category page based on a meta_key (EventDate which is in the form YYYYMMDD), which is working. However I'm struggling ...
0
votes
0answers
8 views

Creating yearly editions based in user interaction

I must to create yearly editions and separate posts of 2018 edition from posts of 2019. I think, editions can be a taxonomy and years (2018, 2019, etc.) can be taxonomy terms. Then, I can ...