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
14 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 ...
  • 137
0 votes
0 answers
35 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 ...
  • 25
0 votes
2 answers
48 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() ) ...
  • 101
2 votes
1 answer
38 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 ...
0 votes
0 answers
35 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 = (...
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: ...
  • 5,665
1 vote
1 answer
83 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 ...
  • 11
0 votes
0 answers
33 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&...
  • 101
0 votes
1 answer
72 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() &...
  • 31
0 votes
1 answer
50 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->...
0 votes
1 answer
137 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 ...
  • 181
0 votes
1 answer
74 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 ...
  • 186
0 votes
1 answer
47 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. ...
  • 25
0 votes
2 answers
125 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, ...
0 votes
0 answers
48 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-&...
0 votes
1 answer
67 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 ...
0 votes
1 answer
155 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, ...
0 votes
0 answers
142 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 ...
0 votes
0 answers
15 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 ...
  • 1
0 votes
0 answers
342 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 ) { ...
0 votes
0 answers
93 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 ...
0 votes
0 answers
13 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 ...
  • 824
0 votes
0 answers
133 views

Woocommerce pre_get_posts query variation meta data not working

In wordpress I made a simple plugin that adds a function to the pre_get_posts hook. I need to be able to search products by their variation sku. The query seems to work as when I var_dump the posts ...
  • 1
3 votes
2 answers
476 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, ...
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. ...
0 votes
0 answers
62 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)...
  • 13
0 votes
1 answer
85 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 ...
0 votes
2 answers
311 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 ...
0 votes
1 answer
331 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 ...
  • 1
0 votes
1 answer
40 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 ...
0 votes
0 answers
50 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. ...
  • 11
1 vote
1 answer
135 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->...
  • 317
0 votes
0 answers
57 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() && $...
0 votes
0 answers
102 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 ...
  • 1
0 votes
1 answer
143 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('...
  • 2,797
0 votes
1 answer
154 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 ...
0 votes
1 answer
113 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 ...
0 votes
2 answers
140 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 ...
  • 281
0 votes
0 answers
139 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 ...
  • 59
0 votes
1 answer
61 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 ...
  • 25
0 votes
1 answer
185 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 ...
  • 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 ...
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 ...
  • 3
0 votes
2 answers
296 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 ...
  • 230
0 votes
1 answer
46 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 ...
  • 13
0 votes
1 answer
119 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
1 answer
477 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
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, ...
1 vote
1 answer
72 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
1 vote
1 answer
575 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 ...

1
2 3 4 5
11