Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take 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
2 votes
2 answers
60 views

Using pre_get_posts on a specific core/query block

I’m trying to apply the pre_get_posts to a specific core/query block, in order to programatically modify the number of posts displayed when a custom attribute is set. If I use the render_block hook, ...
user avatar
0 votes
1 answer
23 views

how to show records that don't have custom meta value

Running pre_get_posts, I want to order the items by a custom meta value. The problem is that by default this doesn't exist. Only if a user performs a certain action does the meta key/value get added. ...
user avatar
0 votes
0 answers
12 views

Exclude post with taxonomy and from the terms (taxonomy & tag) posts count

Using the below code, i have excluded the taxonomy (slug:expired) from archive or tag or taxonomies pages in front-end. add_action('pre_get_posts','custom_get_posts'); function custom_get_posts($query)...
user avatar
  • 13
0 votes
1 answer
44 views

Query multiple taxonomies with pre get posts

I am trying to query a custom post type based on what a user inputs into a form, I am having trouble querying multiple taxonomies in pre_get_posts filter, I can't seem to find a way to set the query ...
user avatar
0 votes
2 answers
32 views

Override tax_query with pre_get_posts to include other term_ids on a single category

I'm trying to include more than one term_id(multiple checkboxes filter) on a single category page. I managed to recollect enough to build a tax_query with pre_get_posts, but now it seems, I have two ...
user avatar
0 votes
0 answers
17 views

How to use pre_get_posts on archive page custom post type

I'm trying to use pre_get_posts to modify my post display request. The posts I want to display are custom post types. I am in a custom page template. I have this is my functions.php function ...
user avatar
  • 1
0 votes
0 answers
19 views

WordPress custom search, search by meta key

I have a search form with two dropdowns showing custom terms and two text fields for custom meta keys, The search work with tax_query but meta_query has no result. Here is the pre_get_posts hooks ...
user avatar
0 votes
0 answers
14 views

`offset` WP_Query argument dont work via `pre_get_posts`

I have some weird problem because pre_get_posts filter completely ignoring offset argument. Here is function from functions.php file: function custom_category_query (WP_Query $the_query) { if ( !...
user avatar
  • 145
0 votes
0 answers
25 views

"pre_get_posts" orderby custom date field in different format?

I can set the query just fine for posts that have the date stored in the database in Y-m-d format, but I have a custom post type where dates are stored as n/j/Y (month/day/year), and would like to ...
user avatar
  • 281
0 votes
0 answers
52 views

How do I display main query posts in random order using add_filter

So I tried two methods to simply display the main query posts in random order - simple. I'm also trying to understand all this better. Using the_content add_filter, with my own instance of WP_Query ...
user avatar
  • 1
0 votes
1 answer
25 views

Replace wp_query with wp_user_query

I'm turning out some subscriber archives and singles (profile pages) in my plugin where I'd also included a 'user-single' template, included via template_include. I'm experimenting however with ...
user avatar
0 votes
0 answers
24 views

problem with setting tax_query in pre_get_posts

I tried to make an advanced search which shows posts in a custom post type (book) with custom taxonomy (writer) that user wants. This code works in a query in a page but didn't work in pre_get_posts. ...
user avatar
  • 11
1 vote
1 answer
30 views

Show scheduled posts in main loop but not in WP_Query?

The blog page should show scheduled posts in the main loop. I've done that with this code: /** * Show scheduled posts in main loop */ function fa_show_scheduled_posts( $query ) { if( $query->...
user avatar
  • 155
0 votes
0 answers
43 views

Merge CPT Taxonomy and Post Taxonomy in $query->set

I'm merging blog posts and a custom post type into a single query and displaying them on my feed as well, here's the code: function add_query_news_projects( $query ) { if ( is_home() && $...
user avatar
0 votes
0 answers
21 views

Order posts by taxonomy terms

I have a cpt archive and want to order it by custom taxonomy terms ids. I'd like to change query with pre_get_posts if it's possible. The desired way is: Taxonomy term id 1 post id 1 post id 4 ...
user avatar
0 votes
0 answers
46 views

Block internal search queries with pre_get_posts and regex rules

This relates to my previous question: Internal search spam (UPD: I no longer use Ivory Search) The closest question I found: Block search keywords As I understood, I need to use pre_get_posts, but I ...
user avatar
  • 1
0 votes
1 answer
60 views

Main menu not appearing in custom post type archive

My site's main menu shows everywhere except on the archive of a custom post type "object". This is caused by this pre_get_posts callback. If I remove it, the menu shows up: add_action('...
user avatar
  • 2,700
0 votes
1 answer
34 views

Detect Ajax call inside pre_get_posts function

In the front end, I have a custom post type archive page with an AJAX Load More button. The loop of the archive page is ordered by date and ASC. I use the same args for the AJAX loop inside a ...
user avatar
0 votes
0 answers
10 views

Search custom posts by meta key in admin

I want the wordpress search in the custom post admin to include searching on a meta key not just the post title. I have the below which works for searching the meta key. But it stops the search on ...
user avatar
0 votes
1 answer
75 views

Main query disturb pre_get_post by category query

I've an archive page with custom post sorted by a custom field. I have two buttons that apply a filter by custom category. The result is not as desired. The correct number of results comes out, but ...
user avatar
0 votes
2 answers
62 views

Posts sortable column not sorting properly for custom field numbers

I have a custom field for view counts that are stored as numbers. I added a Views column, and made it sortable, but they are not sorting properly. The results when I click on the Views column are ...
user avatar
  • 281
0 votes
0 answers
48 views

Make a products only viewable to a specific user ID in meta_query pre_get_posts

I need to show some products to specific users set in the product itself by an ACF field that stores the user ID. I want to hide the product from the product category page / shop completely unless ...
user avatar
  • 61
0 votes
1 answer
37 views

meta_query Array

(by using $query->set( 'meta_key', 'start_date' ); or $query->set( 'meta_query', $meta_query_array );) ... how can I send a array of dates in order to wordpress understand that the post as ...
user avatar
  • 23
0 votes
1 answer
50 views

Woocommerce: Complex query in pre_get_posts [closed]

I want to add a complex filtering when getting my products. I have products where some of them are linked to a custom post type. This custom post type has a repeater field. Depending on a cookie, I ...
user avatar
  • 103
0 votes
0 answers
22 views

unable to customize query to get filtered products by meta_key

i am new to wp and I tried to filter product inside archive-product.php woocommerce shop page by using product meta key and value but unable to do that. Wordpress version i am using 5.8.1 and ...
user avatar
0 votes
1 answer
21 views

Order resultset by configured value and then list all the rest

I want to list jobs (jobs are custom post types) in country order. The country to prioritise in the resultset is defined by the user in the WP CMS. With the configured country being listed first I ...
user avatar
  • 3
0 votes
2 answers
74 views

Tax query with multiple terms in pre_get_posts

I am trying to use a tax query in my pre_get_posts function. All is working but not when the array has multiple IDs. Let's say my URL looks like: ?listing_cat[]=1&listing_cat[]=2 Now I am trying ...
user avatar
  • 230
0 votes
1 answer
43 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 ...
user avatar
  • 13
0 votes
0 answers
53 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 ...
user avatar
  • 123
0 votes
1 answer
48 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 ) { ...
user avatar
0 votes
1 answer
148 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 ...
user avatar
0 votes
0 answers
35 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, ...
user avatar
1 vote
1 answer
47 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
user avatar
1 vote
1 answer
123 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 ...
user avatar
0 votes
0 answers
23 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 ...
user avatar
0 votes
0 answers
278 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/...
user avatar
0 votes
0 answers
38 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 ...
user avatar
0 votes
0 answers
19 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 ...
user avatar
1 vote
1 answer
50 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 ...
user avatar
0 votes
0 answers
153 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 ...
user avatar
  • 9
0 votes
1 answer
69 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 ...
user avatar
0 votes
1 answer
334 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 "...
user avatar
0 votes
0 answers
195 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=&...
user avatar
  • 144
0 votes
1 answer
183 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 ...
user avatar
0 votes
0 answers
65 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() ) ...
user avatar
  • 144
0 votes
0 answers
31 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 &...
user avatar
1 vote
0 answers
66 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', $...
user avatar
0 votes
1 answer
141 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() /...
user avatar
  • 11
1 vote
2 answers
65 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 ...
user avatar
  • 623
0 votes
1 answer
63 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 ...
user avatar

1
2 3 4 5
10