People who code: we want your input. Take the Survey

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
23 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 ...
0
votes
1answer
17 views

date_query in pre_get_posts out of memory

I'm trying to apply a Datefilter for specific categories. Wordpress crashs with a out of memory error. Is this a server problem or is my query wrong? At first I tried query -> (...) but with ...
0
votes
1answer
25 views

Woocommerce products search with custom fields

i have a woocommerce e-shop website. My search is searching only in product title, short description and SKU fields. I want to search also in some custom fields (like "_barcode" and "...
0
votes
0answers
71 views

Excluding posts from search results page with meta query not working

On my search results page I have two loops. First loop displays post by meta value and second loop is "regular" loop that display posts by search query: <section> <div class=&...
0
votes
1answer
27 views

How I can change the condition or compare operator for WP_Query in pre_get_posts

So here is my problem I want to show Posts if have same category if not have post then show post of same tag. And to do that I am using "pre_get_posts" action. and setting query like ...
0
votes
0answers
48 views

pre_get_posts hook not targeting search results page query

I'm trying to target search result page query with this code: function my_custom_search_query( $query ) { if ( ! is_admin() && $query->is_search && $query->is_main_query() ) ...
0
votes
0answers
17 views

How to sanitize my GET string before using in post query

My question is about sanitizing user input data before using it. I am not very experienced in neither PHP or this sanitizing subject so I apologise in advance if this question might come across as ...
0
votes
0answers
22 views

Sort by DESC works, ASC doesn't?

I've looked but don't think this is a duplicate question. So I have custom posts ('Events') that are loading into the loop fine, I am using ACF to give the posts a custom field 'ev_date'. I am using &...
1
vote
0answers
57 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', $...
0
votes
0answers
12 views

Order By Custom Field, otherwise by Date?

I have two CPT and want to order them with Events first, then Articles. Events should be ordered ASC by wpcf-date_time - the event's date, which is in the future. The Articles should be ordered by ...
0
votes
1answer
18 views

pre_get_posts pagination not working

I've been trying to fix my pagination on for category pages but am at a loss. This is my pre_get_posts function for category pages. add_action( 'pre_get_posts', function ( $q ) { if ( !is_admin() /...
0
votes
0answers
23 views

Updating post count on admin page after pre_get_posts filter

After the pre_get_post filters out the past posts, the number of posts above the page is still the original count of all posts (custom post types). How can I update that number so it'll correspond ...
1
vote
2answers
37 views

Formulate a url to show posts with both taxonomy terms

I would like to show an archive of posts that have two taxonomy terms in common. So for example, I'd like to show all posts that have both the terms "sauce" and "cheese" in the ...
0
votes
1answer
14 views

Query to Exclude Child Pages from Custom Post Type Archive

I have a hierarchical custom post type called "Services". On the Services archive page I want to display only parent Services and exclude child Services. I found similar answers to my ...
0
votes
0answers
10 views

Using pre_get_posts filter to reset main query

How can I use pre_get_posts filter to reset main query? I want to use the filter in a page and reset the page query data and make a new query to display the latest posts.
0
votes
0answers
21 views

Order column custom date using pre_get_posts

How I want to sort my custom column contain string date with format j F Y my column data contains string example "21 januari 2021", how I can change the format to date so I sort it. My value ...
0
votes
0answers
12 views

Arranging products in Custom Taxonomy Archive by Category with new WP_Query

I'm using a shortcode and thus using a new Wp_Query - but If I could do it with pre_get_posts in a shortcode I'd also be happy. On a custom taxonomy page "range" I need to group products by ...
0
votes
1answer
15 views

How to display remaining post ( in post__in ) if posts are less then post per page?

I am sorting product on product category page. I want to display sale product at first and show remaining product after it. I am using pre_get_posts hook to modify query. My CODE function modify_query(...
0
votes
0answers
34 views

Changing posts per page on home page breaks pagination on home only

I'm pretty much a beginner here and have a bug in my website. I have a grid layout of 4 x 4 with a static post as my first one on the home page. It displays on the homepage only using <?php if(...
0
votes
0answers
15 views

How to remove scheduled posts from all posts in admin?

How can I remove scheduled posts from all posts list in admin? I am always scheduling posts in the future and if they are many shown on all posts it is a mess to work with. The URL for it is BOTH ...
0
votes
0answers
28 views

meta_query logic with query

I'm currently building a pre_get_posts function that will do the following: Filter out from the filters that I have set up but also I need some date filtering logic, for example if I have a project ...
0
votes
0answers
19 views

Display posts between two dates meta_query wordpress

I have two custom fields (advanced custom fields) of "start_date" and "end_date", I want to filter the projects that are between the two given dates, here is what i've tried: ...
0
votes
0answers
38 views

building filtering system

I have a custom 'pre_get_posts' function, that attaches meta_query: function my_pre_get_posts($query) { $query_filters = array( 'color' => 'color', 'size' => 'size', 'type' ...
0
votes
0answers
8 views

How to easily exclude first post in pre_get_posts only in queries other than page 1?

In home/category pages (blog), I need to have 4 posts in first page, then 3 posts in all other pages. Problem is, I cannot simply alter posts_per_page, because in following pages I would have wrong ...
-1
votes
2answers
73 views

How to ignore posts in pre_get_posts?

I'm using pre_get_posts and looking for a way to exclude posts in the main query. I'm using query_vars to query posts from a specific category and looking for a solution to exclude those posts in the ...
0
votes
1answer
36 views

custom sorting media with multiple filters - error: not unique table/alias: wp_postmeta

I tried to add a column "Downloads" in the media library and make it sortable. This all works fine, but as soon as I search for a filename next to the sorting, the following error occurs: ...
0
votes
0answers
12 views

Including others post width pre_get_posts and meta_query on archive page tag

I have a problem with a pre_get_pots function for the archive page tag (is_tag()). I want to add some posts to diplay on the archive page tag, with meta_query, corresponding to a meta field (of post). ...
0
votes
0answers
16 views

pre_get_posts WP_User_Query filter

I’m trying to run a custom user search, which filters via an array set in ACF. Essentially, I’m storing a list of users (contacts) in an array, and, if that is set, I want to search through those ...
0
votes
0answers
25 views

meta_query is overriding default search

I'm using default search with get_search_form() function. I want to search also by custom fields, so I wrote a hook: function search_products($query) { if (is_search() && $query->...
0
votes
0answers
55 views

Custom posts per page on first page generates wrong number of pagination links

I've looked at every solution I could find, but everything I've tried seem to have this same issue (although nobody has mentioned it). I have a custom taxonomy with 30 posts in it. Global ppp is 9, ...
0
votes
0answers
33 views

Filter search results by post_tag if exist

I want to get posts from search results using post_tag. for example If a user input in the search form "party". First, we search if "party" exists in post_tag and show all posts ...
0
votes
0answers
56 views

Random order not working correctly when using default loop + $query->set

I'm trying to use orderby => rand on a custom taxonomy page, but it's not working correctly. On other pages I use below piece of code to show my posts from a custom post type: $paged = ( ...
0
votes
0answers
16 views

set orderby to main query using pre_get_posts hook

I am using pre_get_post to alter the orderby of the query and I am not getting the expected result. This is my code: function dl_pre_get_posts( $query ) { if ( is_admin() || ! $query->is_main_query(...
0
votes
0answers
18 views

post count not reflecting pre_get_posts action

please can someone help me out! I'm using a plugin to create a widget for a custom post type archive which is showing in a sidebar. I then added a function to further filter the results using ...
0
votes
0answers
72 views

How do I troubleshoot pre_get_posts with Debug Bar?

I am using pre_get_posts() on a custom post type Archive page, to change the sort order. When I view the query using Debug Bar and WP Query Monitor, I can see that my action is indeed firing. If I ...
0
votes
1answer
72 views

pre_user_query vs pre_get_posts

I've used pre_user_query to edit the actual sql query and it works wonderfully. Now I'm trying to work with pre_get_posts and its not at all the same thing. I need to do an INNER JOIN.. In this ...
1
vote
0answers
20 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-...
0
votes
2answers
38 views

Can I alter the main loop to 'orderby' a custom callback?

I have a custom post type where the title is a name, and I want to sort the loop by the last word in title, the surname. I can easily break the names apart and filter by the last word in a custom ...
1
vote
0answers
22 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
21 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 ...
1
vote
1answer
176 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
28 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
36 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
16 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
1answer
353 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
27 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
48 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
738 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
2answers
41 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 ...

1
2 3 4 5
10