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.

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 ...
1
vote
0answers
106 views

Tax query in pre_get_posts not working

I'm trying to filter the main query on the home.php template so I only shows posts from the latest issue taxonomy. What I have so far is the below but it is still showing all the posts on the ...
1
vote
0answers
93 views

Why the pagination for a query modified by pre_get_posts doesn't mind the number & order of posts sorted & selected based on a custom date time field

I have the following task I'm trying to accomplish. I want to display events in the post post type. I want to sort them by date and time and there should be events only displayed happening right now ...
1
vote
0answers
22 views

hide custom post types with specific meta key ON admin backend

I got a tricky problem I need to solve. So say, you have a custom post type of event, then this event has a scheduled event date by a custom field. And this event is recurring, like every Saturday ...
1
vote
0answers
66 views

Add custom function to child function.php

I am trying to add a custom function to remove a particular category from my blog page of the website. I got the code right in parent function.php but when I try to add the same code to my child ...
1
vote
0answers
111 views

Use custom post meta in permalink

I have a random alphanumeric ID assigned to each post as a post meta item, and am looking to use that in the permalink. I've found something that works, but it breaks other posts and pages. For ...
1
vote
0answers
244 views

Custom Taxonomy Not Working When added to WordPress Search

I have created custom taxonomies for a custom post type. I have added drop downs to the search form so I can add the custom taxonomies to the search criteria. The problem I am having is that when my ...
1
vote
0answers
460 views

Applying posts_clauses filter to specific queries only

I've written a plugin where I need to use the posts_clauses filter on WP_Query in order to do my own JOINs and write some complex WHERE conditions that WordPress is unable to generate using the ...
1
vote
0answers
140 views

Order ascending is ignored in meta query?

I'm using pre_get_posts to alter my main query and attach a meta query to it. This is on a woocommerce products archive page (...not sure if that would make a difference). function has_upcoming_date(...
1
vote
0answers
20 views

Change the main loop Wordpress impact on the server?

I used pre_get_posts to change the loop of my Wordpress site. The idea of this loop is not show posts I define an expiration date, whose dates are added in a custom field ( postexpiry ) . These ...
1
vote
0answers
233 views

Why does pre_get_posts() return “date” as the orderby parameter for every sortable column?

I am stuck trying to get a custom column to sort according to a custom field. I have two custom post types: Brand and Product. I would like the Product admin screen to have a "Brand" column, and for ...
1
vote
0answers
600 views

pre_get_post alter current post id

For some purpose, we want to apply a change to the Wordpress flow that affect's WooCommerce product page being displayed. When a product page is displayed (e.g. www.mysite.com/?product=my-product), ...
1
vote
0answers
542 views

New WP_Query calls Pre_get_posts filter twice

On my WP theme template page, I am running a new WP_Query. It seems to be calling my pre_get_posts filter twice, which then wrecks my custom parameters. The second time the pre_get_posts filter is ...
1
vote
0answers
584 views

Removing taxonomy query by pre_get_posts

I would like to remove taxonomy related SQL query part from the main query. I am trying to use pre_get_posts posts_where filter. An example of original not modified query WHERE part: WHERE 1=1 AND ( ...
1
vote
0answers
342 views

Using different parameters for different queries with pre get posts in functions.php

Im using pre_get_posts for my taxonomy.php for my products and paginations to work properly. And I have my pre_get_posts in my functions.php. This is how that current code looks in my functions. ( ...
1
vote
0answers
610 views

Remove posts inside pre_get_posts using a custom query

I need to remove posts from admin listing, if their last child post author is different than the parent post author. I was hoping to use pre_get_posts, but can't figure out how to do this. Here's my ...
1
vote
0answers
1k views

Removing “s” from search with custom parameters

I'm trying to do something that I thought would be quite simple and has turned out to be not simple at all. I've seen quite a few posts that look similar at first glance but none solve the issue for ...
1
vote
0answers
221 views

Filter pre_get_posts does not modify Mine/All/Pending

Maybe I need to use a different filter, but I am using the pre_get_posts filter to filter out posts on a custom post list view. It works for the main table, but the quicklinks to (All) (Published) (...
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 ...
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 ...
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 ...
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 ...
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
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
39 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. ...
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 ...
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 ...
0
votes
0answers
34 views

Modify a query with no results in pre_get_posts

I'm looking for a way to ensure a query always returns posts, even if the original query was empty. The query is a custom post type archive query, and my goal is to change a query var if the query has ...
0
votes
0answers
38 views

Why does this incorrect pre_get_posts meta_query work for sort order?

I've got a fairly complicated post type archive page that I need to order by two meta fields and the post title. To do that, I'm using pre_get_posts with a meta_query that simply tests for the ...
0
votes
0answers
21 views

Sorting meta_query of custom post type

I have 2 custom post types Transit and Colors, connected with a relationship - as a transit post has a custom field to select a color post that it will belong, that stores the Color post ID. I ...
0
votes
0answers
94 views

WooCommerce pre_get_posts meta_query no results message

I have implemented a product dimensions filtering system, which appears to "mostly" work. I pass a min & max set of dimensions for the width and/or height of the products via query string. Then, ...
0
votes
0answers
26 views

All sticky posts are displaying on home.php and ignoring tax_query

I'm trying to query the main loop on my home.php template to display posts which are only in a specific taxonomy 'issue-41'. I'm using the pre_get_posts function to achieve this but when testing it, ...
0
votes
0answers
30 views

Custom Order Current Query: By Meta Key and Category

I recently posted a similar question, but it didn't seem anyone could help - I've thought of a simpler method below if anyone can offer any advice? Been trying for 3 days on this with no luck, am sure ...
0
votes
0answers
48 views

Pre_get_post custom taxonomy combined with custom fields?

So I have the function below to filter posts by custom terms and custom fields. The custom taxonomy filter works good. The custom field filter works as well, which actually shocked me, since I don't ...
0
votes
0answers
31 views

CPT year wise archive based on custom date Field

I am trying to group( archive ) my CPT (fcra_donation : has_archive => true) based on a custom date Field ( sukritham-donation-date : format yyyymmdd (20160506). Here is my code in my functions.php ...
0
votes
0answers
187 views

Complex query using pre_get_posts

I am trying to alter the query for the exam custom post type in admin using pre_get_posts. My scenario is that there are students and teachers roles. Every student can save their preferred teachers ...
0
votes
0answers
366 views

Weird problem with pre_get_posts and $query->is_page()

The following bit of code was used in my fictitious plugin to redirect the non-logged in users from page 173 (ID) to sample-page (slug). The code's working good. But just today, I figured out that the ...
0
votes
0answers
185 views

why doesn't this pre_get_posts code work?

I'm trying to use pre_get_posts to modify the query so that only schools that give student discounts are displayed on the archive page. The field in question is called 'student_discount' and is a ...
0
votes
0answers
31 views

What's the most efficient way to get two queries based on an if statement?

I want to retrieve all custom posts with a specific custom taxonomy term, and order them by a particular custom meta field. However, I also need to cater for when the orderby field is empty, and in ...
0
votes
0answers
95 views

Override main query for page template

I am using a page template to loop through posts there is already a main query, however, before the posts are displayed I only want the unexpired posts to display so I am using this code to add a ...
0
votes
0answers
33 views

New custom post type entries are not sorted correctly in admin using pre_get_posts

I am working on a WordPress plugin and trying to use the pre_get_posts hook. Everything seems to sort, but when I add new items through the back-end, they get added to the bottom and not sorted for ...
0
votes
0answers
42 views

Testing requested query in pre_get_posts

Depending on user roles, I am hiding admin posts in dashboard->edit.php, however if non-admin user requests to list it's own posts this should be allowed. To achieve this I am trying to test what was ...
0
votes
0answers
56 views

Custom Post Search

I'm trying to modify search results for my custom post type. I have clarified that the code I am using is returning users and post ID's but no results are displayed. Essentially when a search is ...
0
votes
0answers
136 views

WP search in metadata post

In want to search by meta data value in post but I didn't succes. For example if I type "sound" I want to found "sounddiary" for example. Here my code: function custom_search_query( $query ) { ...
0
votes
0answers
618 views

pre_get_posts with multiple post types AND a meta_key

So I am trying to customize the search a bit. The client wants the main site search to return people, projects, and posts. Not normally a problem. However for projects, only projects with a certain ...
0
votes
0answers
87 views

wp_query - Modify $query to include duplicate content

I'm setting some featured content through the customizer (it's saved in theme_mod); Using the pre_get_posts hook, I'm retireving the feature pages in this way: add_action('pre_get_posts','...
0
votes
0answers
260 views

Include attachments with a custom taxonomy in search

I need to include attachments in the search. So far, I have managed this by hooking into pre_get_posts and checking if it's on the search results page, I add 'attachment' to the post_type and 'inherit'...