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.

Filter by
Sorted by
Tagged with
0
votes
0answers
16 views

Get posts that are parents which don't have any children with WP_Query

I have a hierarchical custom post type which has posts with no children and also posts that have one or multiple direct children (only up to a depth of 1, i.e no children of children). For example: ...
1
vote
0answers
18 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 ...
0
votes
0answers
16 views

query_posts in pre_get_posts removes all body classes - how can I reset or keep them?

I've created a function that searches images for a tag, uses them to create a loop and build an array of their parents' IDs. Its in a pre_get_posts. Its all working fine except I'm obviously ...
0
votes
1answer
18 views

Change archive page template using pre_get_post

I've used pre_get_post to modify and display my custom post type in an archive page with pagination which uses my custom created archive template, see here. It it displaying the posts exactly what I ...
0
votes
0answers
12 views

Multiple meta_key ordering with numbres

I am trying to create a search with different search terms and it works correctly for me. The problem is that these fields are a string and not a number, so it does not order me correctly (for example,...
1
vote
1answer
11 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 ...
0
votes
1answer
12 views

Setting user permissions per post

I am trying to assign privileges on posts on a per post basis. Essentially, I have a custom post type which have authors. They can edit their own posts. I then have another role, which should be ...
0
votes
0answers
12 views

WP_Query orderby meta_value Speed

I currently sorting my posts using this code: function main_query($query) { if ( is_search() || is_home() || is_front_page() || is_archive() ) { if (!is_admin() && $query->...
0
votes
0answers
17 views

Show only posts without format in admin screen

The situation In admin screen (edit.php?post_type=post) using format selection dropdown adds a URL parameter like post_format=video. This makes the query to only look for posts with post-format-...
0
votes
1answer
12 views

How to exclude woocommerece product category in search results?

I'm looking for a way to exclude a product category from search results with Woocommerce using the pre_get_posts action and $query->set(). I am searching using the Wordpress ?s=keyword search. I know ...
0
votes
1answer
13 views

Prevent A Specific Custom Post Type Showing In WP Search Results Page

I've excluded certain pages from the WP Search with the code below in my functions.php file. I also need to exclude a custom post type called 'sites'. I can't seem to find the custom post type ...
0
votes
1answer
21 views

Can a link in WordPress contain a query string that is picked up as $_POST

This question is about a very specific circumstance. The site has one template file that can run a default category query with thumbnail, linked title and excerpt, or it can run a linked title only ...
0
votes
2answers
34 views

pre_get_posts and search query for admin

I'm going round in circles with trying to include several custom fields in my search results. The custom fields are managed via ACF. At first I want this for the admin. /wp-admin/edit.php?post_type=...
0
votes
0answers
10 views

Alphabetical taxonomies and pre_get_posts not working

I am currently building a website that makes use of a theme. The theme has a custom taxonomy called team_group. I created a custom template called taxonomy-team_group.php to be able to list the posts ...
0
votes
2answers
29 views

order posts by a secondary query that counts items

i'm working on a music plugin. It uses a custom table tracklist_items to store every tracks of a playlist; which has a structure like this : |id| |track_id| |tracklist_id| |order| |time| So if I ...
0
votes
1answer
24 views

Problem ID to exclude specific posts from category

I'm trying to exclude specific posts from category/archive with these functions: function exclude_single_posts_cat($query) { if ( is_admin() || ! $query->is_main_query() ) return; ...
4
votes
1answer
65 views

How to exclude latest x posts from a paginated query?

I have a WordPress loop, that is part of an AJAX call. Every time a button is clicked, this function is called, runs the below loop and outputs the next 6 posts. There are already five posts shown by ...
0
votes
0answers
13 views

how to exclude specific pages form wordpress search results?

I want to show only today's events and the next events in Worpdress search results for every post with category "events". This is my code and it doesn't work, the events that have ended still appear ...
0
votes
1answer
29 views

How to use pre_get_posts

I'm a little stuck on how this hook is used. Does it go in the search template, the archive page, or in functions? Does it surround the pagination code? Also, does it work differently when I'm ...
0
votes
0answers
51 views

pre_get_posts causings DB error when using ( 'posts_per_page', -1)?

Has anyone encountered a DB error when using pre_get_posts to display ALL posts on a CPT archive page? This error... WordPress database error: [You have an error in your SQL syntax; check the manual ...
-1
votes
1answer
52 views

WP_query sort by custom meta_key “price”

I've created a custom post field for use on my site that is the price of a real estate item for sale (that is the subject of the post). It should sort posts archives by price but I cannot get it to ...
0
votes
1answer
28 views

cannot filter after using pre_get_posts

I am using pre_get_posts() in the backend for a custom post type screen where all the records are usually displayed. The action I have is showing only posts for today's date. This is working fine ...
0
votes
2answers
28 views

How to change the amount of posts previewed on a page?

I have tried this code, but it doesn't work. function university_adjust_queries($query) { $query->set('post_per_page', '1'); } add_action('pre_get_posts', 'university_adjust_queries');
0
votes
0answers
18 views

Query not ordering items correctly due to non existent values

I am trying to pre_get_posts and set a custom orderby. From searching through other Stackoverflow, WordPress Development questions and answers I managed to build this so far. This works 99% correctly,...
0
votes
1answer
32 views

How to hide private posts even if user is admin

Even if admins, I dont want to view private posts for custom post type. Added the following code. function my_function( $query ) { if ( ! is_admin() && ( is_singular( 'my_post_type' ) || ...
0
votes
1answer
21 views

How can I re-query post_type and rewrite the url?

I have the url slush like the below. /category/land-residential-farm/?property_com_listing_type=sale I want to write the url to /category/land-residential-farm/property-for-sale then re-query the ...
0
votes
2answers
83 views

Wordpress meta query not working

add_action( 'pre_get_posts' , 'my_pre_get_posts' ); Function my_pre_get_posts( $query ) { $value= $_GET['s']; if( $query->is_main_query() && $query->is_search()) { ...
0
votes
0answers
17 views

Search in multiple categories using $query->set

Right now i have this code: function exclude_categorii( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_tax ) { $query->set( 'category__in', ...
0
votes
0answers
27 views

Empty posts return an HTML comment

I'm using the script from this answer to exclude empty posts from the main query. It work well, but since the new WordPress editor, some empty posts return a comment rather than an empty string. i....
0
votes
0answers
17 views

Stumped on filter during pre_get_post for category

I'm attempting to create a filter on a posts archive page. Currently have filters set up for author, category and year and using pre_get_posts to use the same page. Both the author and year filters ...
1
vote
0answers
96 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 ...
0
votes
1answer
47 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
28 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
105 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
1answer
108 views

Compare meta key to current date in pre get post

I'm looking for a way to compare a string to the current time. I found this question "pre_get_posts filter using numeric meta_query comparison (from dates)" and I'm confused as to get it to work. $...
0
votes
0answers
43 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 ...
1
vote
0answers
298 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
31 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'] ) && !...
3
votes
2answers
817 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 ...
1
vote
1answer
519 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 ...
1
vote
1answer
61 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
231 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
153 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
201 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
votes
1answer
16 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
1
vote
1answer
37 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 ...
4
votes
1answer
86 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 ...
0
votes
1answer
507 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
511 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
2 3 4 5
10