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
1answer
29 views

CPT is simply not displayed in the main archive with "pre_get_posts"

I'm trying to get my Custom Post Type to show up in the main archive using pre_get_posts, but it just won't work. However, in the search results the entries are displayed, and they are also displayed ...
0
votes
0answers
17 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
1answer
29 views

queried_object using pre_get_posts gets notices and warning

My code is getting me the result I want on my page, I'm fetching another post and my theme is outputting all of the relevant data I need on the page as expected. function replaceQuery( $query ) { ...
0
votes
1answer
33 views

Sort categories by custom field in WordPress admin

I know there are a lot of threads about similar issues, but I can't figure out why my custom sort won't work in the category admin page. Here is my code to add the ACF field as a column to the ...
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, ...
2
votes
1answer
33 views

How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?

Example: $query->set('s', 'foo bar'); - search foo bar I want set search as foo bar OR lorem ipsum dolor OR ping pong
0
votes
1answer
26 views

Wordpress custom search form with pre_get_posts not work

I am designing my first theme and I am stuck on the search functionalities of a Custom Post Type. I created a CPT 'movies' and created the archive page with the 'archive-movies.php' file in the root ...
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
72 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
16 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 ...
1
vote
1answer
19 views

How to choose between hooking into per_get_posts or into parse_query

In refactoring my code base I noticed there are two hooks that used for modifying the main query. parse_query pre_get_posts From the Plugin API Reference it seems these hooks fire back to back and ...
0
votes
0answers
73 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
30 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
35 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
88 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
34 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
51 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
52 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
24 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
60 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
14 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
31 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
29 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
40 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
20 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
13 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
24 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
17 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
35 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
17 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
29 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
21 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
9 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 ...
0
votes
2answers
78 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
40 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
13 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
23 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
26 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
89 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
94 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
91 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
25 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
46 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 ...
1
vote
1answer
267 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 ...
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 ...
0
votes
1answer
44 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 ...

1
2 3 4 5
10