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.

127 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
3
votes
1answer
108 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
502 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
99 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
243 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
1k 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 ...
2
votes
1answer
171 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
514 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() ){...
2
votes
1answer
159 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 ...
1
vote
0answers
71 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
27 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
122 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
100 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
24 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
165 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
71 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
531 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
41 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
45 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
115 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
252 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
424 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
483 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
254 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
417 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
144 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
1answer
78 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
239 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
488 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
619 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
571 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
397 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
587 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
349 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
1answer
669 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
vote
0answers
625 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
587 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
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
228 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
31 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
1answer
17 views

How to show only specific category post by user role without plugin and restrict all other cats

I am using basic post for adding posts. 5 categories exist ( cat_A, cat_B, cat_C, cat_D, cat_E). And I have 2 custom user_role for registered visitors that is 'viewer_a' and 'viewer_b'. I need to show ...
0
votes
0answers
18 views

Apply pre_get_posts filter in a certain moment (just for a particular block of posts)

I want to apply a filter to a block of posts, for a custom field that I created, called: 'highlighted_in'. To filter the results I'm using the 'pre_get_posts' hook, and it works. But I think my ...
0
votes
0answers
16 views

Search filter inside class

I have a theme class In public function search exclude how to pass arguments? public function searchFilter( ){ function searchFiler($query, $array_ids(10,17){ $query->set(‘post__not_in’, $...
0
votes
0answers
15 views

sort posts on front end using url query

I am trying to sort posts on my archive page based off a custom field. The custom field is a number. I have the following in my functions.php file - function wpse139657_orderby(){ if( isset($_GET[...
0
votes
0answers
13 views

Security in Wordpress, better to edit pre_gets_post or protect single pages?

I am making a website where user must register and when they are logged in they must complete the profile informations with other infos before they can access the content. I wrote this function ...
0
votes
0answers
18 views

Adding post_type filter on pre_get_posts gives 404

I have a basic WordPress query: $the_query = new WP_Query(array( 'post_type' => array('post'), 'posts_per_page' => -1, 'ignore_sticky_posts' => true )); But if I try ...
0
votes
0answers
21 views

PaidMembershipsPro plugin's query filter not working for REST API calls

I have a custom REST API in WordPress that returns a list of posts using the get_posts function. I am using the PaidMembershipPro plugin, which typically filters the get_posts results according to ...
0
votes
0answers
31 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
34 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
49 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 ...