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.

152 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
3
votes
1answer
246 views

Exclude a WordPress post from pre_get_posts if a field is null

I have an CPT archive page that I would like to exclude certain posts from if a field is null. The field I want to look for is title_group and I am already using a different meta_key to order the ...
3
votes
2answers
657 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('...
2
votes
1answer
120 views

Category archive page - loop through posts of certain tag (with pagination) - pre_get_posts

I have a category archive page (press), which I need to loop through posts of a certain tag (magazines). I tried to modify the query with pre_get_posts, but it is still just showing posts in the '...
2
votes
1answer
477 views

Skip first post on Category Archive

I have created a filter to skip first post of the archive on "podcast" category. Below is the code. add_filter( 'pre_get_posts', 'amd_limit_posts'); function amd_limit_posts($query){ if (...
2
votes
1answer
232 views

Two loops by pre_get_post on same page

I have two loops in archive.php, both are affected by pre_get_post. My code on pre_get_post is this: // My pre_get_post function my_pre_get_posts( $query ) { if ( is_admin() || ! $query->...
2
votes
1answer
165 views

When querying a combination of posts and other meta fields, is there a better solution than directly modifying the WHERE value?

This question is a follow up to a question I recently asked. I've implemented what seemed to be the only solution, and it seems to work (YAY!). I just need to verify that: This is really the only ...
2
votes
1answer
2k 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
63 views

How to use $query->set with post__in?

I use the pre_get_posts hook to change the search results because I have a filter form. This hook gives me access to an already made $query object. This line does not work: $query->set('post__in', $...
1
vote
0answers
26 views

How to sort custom columns for custom post type without meta query and pre_get_posts?

I have a custom post type orders and multiple custom columns. I've added the custom columns with the manage_orders_posts_columns hook and registered the columns as sortable with this hook: manage_edit-...
1
vote
0answers
24 views

Sort the result of the main Query in shop page by a custom taxonomy

In a WooCommerce site, we have products that have a custom taxonomy that are assign to a region. When the user visits the site, we activate the browser geolocation, and we assign the user a region in ...
1
vote
1answer
30 views

Replace/Mute/Stop Search Query

I'm trying to replace the search functionality in WP. I've already created the search.php template with all the results I want to display. I don't want any results from the WP database. I just want ...
1
vote
0answers
124 views

Category param redirect to post type archive

I'm trying to filter categories by post type. e.g: the following URI will retrieve all post from the category news. https://example.com/category/news/?post_type=post. Here is how I'm updating the ...
1
vote
0answers
375 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
1answer
36 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 ...
1
vote
0answers
331 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
286 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
76 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
1answer
275 views

WP_Query ignoring tax_query when is_singular

I’m trying to set up some post permissions using taxonomies and I'm applying my rules in a pre_get_posts action. It’s working fine when multiple posts are found, but in class-wp-query.php near line ...
1
vote
0answers
102 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
1answer
2k views

Search by type posts and taxonomy

I want that my search page give results of posts (that i want that show) and taxonomy The code to get just two type of posts: function searchfilter($query) { if ($query->is_search &&...
1
vote
1answer
227 views

How do I hide posts across all loops based on the value of a custom field?

I'm trying to dynamically hide posts across my entire site if the value of a custom field called distribution is "1". I've set up a meta box on the post editor screen to set the value of the field, ...
1
vote
1answer
46 views

WordPress Tag or Custom Taxonomy Return All Posts if has that Word in Post Title

Note : In this question "Tag" means Tag or a Custom Taxonomy (Assume that it is topic). It is hard to explain my question with words. So I will explain it using following example. When user visit ...
1
vote
0answers
255 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
266 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
1answer
570 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 ...
1
vote
0answers
635 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
1answer
378 views

Help ordering Post loop by two meta values

Background: I have a custom post type "events" which has a post meta value of a unix timestamp called event start date. I also have a post meta value of ticket_status that has 3 possible values (S=...
1
vote
1answer
508 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
0answers
149 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
21 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
1answer
101 views

Sort meta-value in the sequence array in pre_get_post

I am customizing the display of posts in a category with pre_get_posts and meta_value . Each will receive a meta_value posts and wish them to be in the order of the array. I want it that way ( 1 ) ...
1
vote
0answers
305 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
1answer
830 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. ...
1
vote
0answers
783 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
753 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
1answer
539 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
vote
0answers
674 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
411 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
775 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
1answer
806 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. ...
1
vote
0answers
2k 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
241 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) (...
1
vote
1answer
72 views

Include Site 1 Posts in Query for Sub Sites

So I have a Primary site that posts announcements about the sub sites, for each of those posts I have a meta value that the user sets for which site those announcements apply too. On each of those sub ...
0
votes
0answers
18 views

Filter search query to exclude postmeta value

In my wordpress site I am making use of the pre_get_posts filter to customise my site search query. In functions file I have the following below. Now my issue is, using FieldManager I have added a ...
0
votes
0answers
33 views

Add terms to a taxonomy archive from within the same taxonomy

not sure if this is even possible (guessing it should be though)... I have a custom taxomomy of status, containing the following terms... pending, collecting, started, doing, dispatched, delivered, ...
0
votes
0answers
19 views

add_rewrite_rule - suddenly stopped working (wordpress)

I have a url example.com/sample-page/?amount=567 It initially worked ok, it redirects to: example.com/sample-page/567 But after few days, i noticed its not working any more. Its now redirecting to ...
0
votes
0answers
93 views

is_user_logged_in not working from WP-API (wp-json)

I want to hide certain posts from visitors that are not logged in. The code below works perfectly when the visitor visit the page but not is I try to see all the pages from the API with wp-json/wp/v2/...
0
votes
0answers
19 views

How to remove term from wp_query in pre_get_posts?

I have a price range taxonomy setup for filtering. Taxonomy terms are like "20-50", "51-70", etc.. URL is mysite.com/products/price-range/21-50/ Products are setup with ACF to have ...
0
votes
0answers
17 views

Search Exact on WordPress - Relevannsi Solution Not Working

I have read a lot of articles about exact keyword query problems in WordPress and how to fix them, even though I have followed them but still no success. My website is under construction about ...
0
votes
0answers
95 views

Tax query get first product with attribute value in pre_get_posts

We've got a webshop with over 3000 products. Some products have the same attribute value for pa_model, as shown below. Productid 1 - pa_model Nike Productid 2 - pa_model Nike Productid 3 - pa_model ...