As of May 31, 2023, we have updated our Code of Conduct.

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
0 answers
9 views

Search for meta_query does not return any result if combined with title

I'm trying to extend the wordpress search to include a custom cpt through the meta value associated using an ACF. All seems to works great, but the main problem's that I have to add this code to make ...
sfarzoso's user avatar
1 vote
1 answer
19 views

How can I incldue a "private" post type in a loop for public users?

I have a post type that I've registered using "public" => false, "publicly_queryable" => true, because I don't want singles, but I do want these posts to show up on a custom ...
JacobTheDev's user avatar
  • 1,171
0 votes
0 answers
10 views

How to exclude first 2 posts from a specific category for a custom post type archive page

I have a custom post type, product with various categories. On the archive page for this post type, I would like to exclude the first 2 posts tagged with the "Featured" category (ID 7) from ...
SleeplessInSublime's user avatar
0 votes
0 answers
20 views

Sorting by Title for Post Archive Categories for Custom Post Type

I have a custom post type - vendors. They have a taxonomy vendor_category. I am attempting to sort the archive pages by the title (since it defaults to post date). Here is the code that I am putting ...
JasCav's user avatar
  • 137
0 votes
0 answers
36 views

Duplicating event posts in wordpress

I'm facing a problem for days now. In order to make a events calendar wp Website (witout plugin), I have a CPT "Event" with a custom field "event_dates". The custom field is build ...
nico's user avatar
  • 25
0 votes
2 answers
63 views

How to put posts with some taxonomy on top of others in `pre_get_posts`

The following tax_query returns only matched posts (with 'matchedstring' IN taxonomy array): function only_returns_matched_posts( $query ) { if( !$query->is_main_query() || is_admin() ) ...
mikmikmik's user avatar
  • 101
2 votes
1 answer
45 views

Custom taxonomy with custom meta value is not sorting correctly (query returns the same value for orderby regardless of sort column click)

I have a weight class category, where I want to sort by weight so Heavyweight shows at the top, and Flyweight at the bottom. I have created a custom meta field called 'weight' where I store an int ...
Mac Cooper's user avatar
0 votes
0 answers
81 views

Hide empty categories on widget

I excluded some products from woocommerce_product_query with this code: function en_archive_products_by_rol( $query ) { if (is_admin()) { return; } $meta_query = (...
Elianalu's user avatar
0 votes
0 answers
30 views

pre_get_posts has php notice when not on CPT archive

I'm setting my post archive page to sort by a meta date. When I'm on the archive page it all works. However on other pages I get a PHP notice Undefined index: post_type. Here is my function: ...
rudtek's user avatar
  • 5,760
1 vote
1 answer
155 views

Sorting a custom post type in pre_get_posts

I have a custom posttype "Talks" which have a custom field name start_date. The posts start_dates are the the past and in the future. I need the future posts to come first with the ...
Jesper's user avatar
  • 11
0 votes
0 answers
37 views

Search query alteration not working for meta values

The bellow code works on category archive pages, it filters out the necessary products. But on the search page it doesn't work. If I uncomment the line //$query->set("s", "chelsea&...
JorensM's user avatar
  • 101
0 votes
1 answer
111 views

Modify a query using both a text field and a true/false AFC field

I am using the following code to modify the CPT "movimiento" query. add_action( 'pre_get_posts', 'mov_vec_archive_page' ); function mov_vec_archive_page( $query ) { if ( !is_admin() &...
xaifu's user avatar
  • 31
0 votes
1 answer
68 views

Call pre_get_posts inside ajax

Is it possible to run pre_get_posts inside the ajax callback? I want to change the query after the ajax run here my code function repeater_dynamic_query( $query ) { global $post; if ( $query->...
Baim Whello's user avatar
0 votes
1 answer
252 views

What is the proper way to use pre_get_post?

In an attempt to learn the proper way of using pre_get_posts, I came across the example below. As the comment goes, it suggests that the default WordPress query for a given URL should return the ...
Motivated's user avatar
  • 181
0 votes
1 answer
104 views

get_the_terms inside save_post gives old terms

When I use get_the_terms inside save_post, I get the old taxonomy terms. This is something that I would expect from pre_post_update, but why does it happens to save_post ? And, more important, how to ...
Tahi Reu's user avatar
  • 196
0 votes
1 answer
59 views

Pre_get_post on CPT archive page

i would change the main query on a CPT archive page the CPT is declare as "events", the name of the archive page I use is called 'archive-events.php" and this simple code won't work. ...
nico's user avatar
  • 25
0 votes
2 answers
155 views

How to use orderby on meta_value when using Pods custom database table storage

I'm struggling to understand how I can use pre_get_post when the post's metadata is not stored in the wp_postmeta table. I use Pods (https://pods.io) to create custom post types with custom fields, ...
Dan Maby's user avatar
0 votes
0 answers
51 views

tax_query in pre_get_posts

I'm trying to add a tax_query for the query of a custom post type single page with this code. function wcua_pre_get_posts ($query) { // Also removed this condition if( is_admin() || ! $query-&...
Jose Adrian's user avatar
0 votes
1 answer
82 views

Amend taxonomy to search in wordpress admin

I am trying to amend a custom taxonomy to a search in the editor area. The taxonomy slug is 'help_cat' and is attached to 'help-center' post types. What I am trying to achieve is very similar with OP ...
tearswep's user avatar
0 votes
1 answer
207 views

WP_Query - How to get all posts of specific days of week by custom field date?

I have a filter on frontend with days of weeks, and I need show only posts that are published on especific day (or days). The date are on custom field day_of_event (the filter is based on this field, ...
rafaelfndev's user avatar
0 votes
0 answers
163 views

$wp_query->found_posts not returning correct value

I am trying to search woocommerce products from DB using wp_query, I have used pre_get_posts action to modify my main query. add_action( 'pre_get_posts', 'custom_query_vars' ); function ...
Talha Arif's user avatar
0 votes
0 answers
16 views

WP_Query, Paginate by Custom Field

I'll keep this short. I'm new to PHP and having a difficult time understanding the best pagination practices for WP_Query. (I have gotten pagination working, but only by posts_per_page.) Naturally I'd ...
XLIME's user avatar
  • 1
0 votes
0 answers
454 views

Elementor posts custom query based on tag [closed]

How can I filter the Elementor Posts widget with a custom php query that returns posts with a specific tag attached? I want to do something similar to this: function my_query_by_post_types( $query ) { ...
ChristosDg's user avatar
0 votes
0 answers
113 views

pre_get_posts fix to show menu on CPT Archives broken WP 6.0

Before WP 6.0 we have had to add this pre_get_posts fix to show the wp_nav_menu on custom post type archive and taxonomy pages: wp_nav_menu not appearing for a couple pages Here is an example of the ...
Joshua Payne's user avatar
0 votes
0 answers
15 views

Exclude taxonomy Query if post_type

How can I define in pre_get_posts hook on custom post type to not execute the query on a taxonomy. I have the following if (!empty($newsType) && $newsType == 'press') { //if $newsType ...
fefe's user avatar
  • 824
3 votes
2 answers
611 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, ...
Mark Howells-Mead's user avatar
0 votes
1 answer
28 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. ...
user8463989's user avatar
0 votes
1 answer
99 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 ...
grzybowski1911's user avatar
0 votes
2 answers
374 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 ...
steakoverflow's user avatar
0 votes
1 answer
403 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 ...
Mario's user avatar
  • 1
0 votes
1 answer
55 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 ...
Kevin Nugent's user avatar
0 votes
0 answers
55 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. ...
ali's user avatar
  • 11
1 vote
1 answer
176 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->...
Elizabeth's user avatar
  • 327
0 votes
0 answers
58 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() && $...
douglaskarr's user avatar
0 votes
0 answers
105 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 ...
Terry's user avatar
  • 1
0 votes
1 answer
158 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('...
pixeline's user avatar
  • 2,807
0 votes
1 answer
182 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 ...
Mathieu Préaud's user avatar
0 votes
1 answer
116 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 ...
Joan Chaparro's user avatar
0 votes
2 answers
167 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 ...
Michael's user avatar
  • 281
0 votes
0 answers
175 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 ...
Jake's user avatar
  • 59
0 votes
1 answer
62 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 ...
nico's user avatar
  • 25
0 votes
1 answer
204 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 ...
jens_vdp's 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 ...
Amanpreet Singh's user avatar
0 votes
1 answer
28 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 ...
MarkW's user avatar
  • 3
0 votes
2 answers
337 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 ...
RobbTe's user avatar
  • 230
0 votes
1 answer
48 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 ...
anyway's user avatar
  • 13
0 votes
1 answer
152 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 ) { ...
Brendan Jackson's user avatar
0 votes
1 answer
563 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 ...
Meghan Prestemon's 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, ...
richerimage's user avatar
1 vote
1 answer
77 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
Мегакот's user avatar

1
2 3 4 5
11