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.
516
questions
0
votes
0
answers
10
views
Search Filter by mime type not working in pre_get_posts
I'm trying to create a search option for users to display only PDF attachment posts. The following works to display all attachments:
function CustomSearchFilter($query) {
if( (! is_admin() ) &&...
0
votes
0
answers
21
views
Get 404 when using url parameters to filter custom post type archive page created with wp bakery post grid
I created a custom post type named tutors_quiz than I create custom taxonomy named tutor-quiz-tag.
I create an archive page with wp bakery post grid. screenshot https://prnt.sc/VdvDp9-ekt3c
Now I want ...
0
votes
0
answers
23
views
How to Show Standard Post and Custom Post in Same Section at Homepage?
I'm trying to show both my standard posts and custom posts on my homepage. I've got the following code working, but it's causing a 404 error if I assign a custom homepage. If I keep the setting of &...
1
vote
1
answer
54
views
How to set up a private custom post type that is accessible in the administrative dashboard?
I'm not entirely sure of the best way to approach the requirement so I'm hoping someone who has more experience with it will be able to share some gems. I've set up a custom post type with the ...
0
votes
0
answers
13
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 ...
1
vote
1
answer
26
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 ...
0
votes
0
answers
22
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 ...
0
votes
0
answers
114
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 ...
0
votes
0
answers
40
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 ...
0
votes
2
answers
128
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() )
...
2
votes
1
answer
74
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
153
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
31
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:
...
1
vote
1
answer
308
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 ...
0
votes
0
answers
55
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&...
0
votes
1
answer
191
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() &...
0
votes
1
answer
119
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
639
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 ...
0
votes
1
answer
160
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 ...
0
votes
1
answer
96
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.
...
0
votes
2
answers
273
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
65
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
144
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
287
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
625
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
141
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
20
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 ...
3
votes
2
answers
914
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
30
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
1
answer
148
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
609
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
606
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 ...
0
votes
1
answer
99
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
75
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. ...
1
vote
1
answer
298
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->...
0
votes
0
answers
67
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
124
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 ...
0
votes
1
answer
205
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('...
0
votes
1
answer
243
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
123
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
231
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 ...
0
votes
0
answers
221
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 ...
0
votes
1
answer
76
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 ...
0
votes
1
answer
261
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 ...
0
votes
0
answers
24
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
31
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 ...
0
votes
2
answers
586
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 ...
0
votes
1
answer
49
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
1
answer
209
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
789
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 ...