Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
1answer
19 views

Pre get posts where template is not equal to one specified?

I'm trying to not show certain posts in the admin area based on the page template. Here is what I have so far: add_filter( 'parse_query', 'hide_some_posts' ); function hide_some_posts( $query ) { ...
1
vote
1answer
37 views

Get .subsubsub count of post per status queried using pre_get_posts

My following query using pre_get_posts shows the Author only certain posts or pages with certain statuses that s/he should only see and of what belongs to him/her. Is there any way I can get a count ...
-1
votes
1answer
11 views

How to pass >= condition filter to my year custom tax_query

I have a custom taxonomy called year. I need to filter posts by pre_get_posts tax_query from year value to to year value. I am searching from one hour now feeling stuck. Please guide me. Thank you
0
votes
0answers
15 views

Change default wp-query using pre_get_posts

I would like to show all the posts of a post format on top of the default wp query and show all the other post formats below them (in the same query) using pre_get_posts. Please note that I need to ...
0
votes
0answers
26 views

Setting a product tag archive page as front page

I'm trying to set a product tag archive page as the front page. Using this code: function set_product_tag_archive_as_front_page( $query ) { // Only filter the main query on the front-end if ...
1
vote
1answer
30 views

Custom post type show on both Home and Date Archive

Found below snippet to pull posts from a custom post type 'pictures' into the home page and date archive page.. Added to functions.php this works for home page: function ...
3
votes
1answer
26 views

How to show more posts on an archive page?

I'd like to show more posts on my custom archive page, but can't seem to figure out the right way to do it. Currently it displays 10, which is the number set in WP Settings and the number I'd like on ...
0
votes
1answer
41 views

Automatically applying a pre_get_posts filter for child categories only

I have a category named "Research" that has a growing list of child categories. This research category has a special template category-research.php that shows a list of its children instead of the ...
1
vote
1answer
50 views

Does pre_get_posts affect REST API responses?

I'm trying to limit the content that is returned via REST API routes to only contain content published by the current logged in user. I'm trying to do this using pre_get_posts which works fine when in ...
0
votes
0answers
50 views

How to offset main query without affecting other queries and backend

I'm trying to offset my main blog query by 3 posts whilst trying to get the pagination to function correctly. I've been following the codex reference here but I'm struggling to get it working as I ...
0
votes
0answers
10 views

Display random posts, favoring posts published close to todays date regardless of year

I would like my start page to display random posts, but put posts published close to today’s date first and posts published far from today’s date last - but regardless of what year the posts were ...
0
votes
1answer
23 views

Admin table Query filtering “WHERE” for custom post type

So I have been searching for a solution on how I can dynamically change a CPT admin table query by filter the result WHERE Status is equaled to some custom status. I have searched using function ...
2
votes
2answers
149 views

RSS feed with specific keyword

I am able to get articles as RSS feed from below url http://wordpress_site/blog/feed I am also able to get articles from BlogSpot with specific keywords as below https://www.yourblogname.blogspot....
0
votes
1answer
27 views

How to change posts order on Category and Tag archives?

WordPress by default order posts in descending order by date. But I want them to be in ascending order by date. How do I turn the order to ascending only for tags and category? I don't want blog post ...
0
votes
0answers
16 views

Sort posts by tags in a category page

Forgive me in advance for my English. To explain, I would sort posts by tags, by providers and alphabetically. I looked at this post but did not understand the steps. I can not do anything. I already ...
0
votes
0answers
27 views

pre_get_posts meta_query for dates interval

I have an "activity" custom post type, that has start and end dates introduced via custom fields. I want to add a meta_query argument through pre_get_posts, to get "activity" posts between two dates. ...
1
vote
1answer
25 views

Posts will not display on page-mypage.php

Something is really tripping me up. I have been used to querying posts via custom queries however based on research lately I have learned that for the most part this is a big no no. So, in testing the ...
0
votes
0answers
44 views

Pagination not working past page 3 on archive page of category

I have a custom post type of "show" and within that custom post type I have set up a taxonomy / category called "venue" which has two options "venue-one" and "venue-two" (slugs). On the two archive ...
0
votes
1answer
26 views

Pre get posts sort by meta key returns no results if meta key does not exist

I am able to sort my posts by a custom field ('tour_length_days'). However if this field does not exist on an existing post then it will return no results. I was hoping there was a way to ...
0
votes
0answers
25 views

Change post category within pre_get_posts

I have written a filter to be called in pre_get_posts to remove posts from a category page based on a meta_key (EventDate which is in the form YYYYMMDD), which is working. However I'm struggling ...
0
votes
0answers
8 views

Creating yearly editions based in user interaction

I must to create yearly editions and separate posts of 2018 edition from posts of 2019. I think, editions can be a taxonomy and years (2018, 2019, etc.) can be taxonomy terms. Then, I can ...
0
votes
3answers
77 views

Pre_get_posts only show posts by administrator roles

I am trying to filter a query to only show posts that are posted by a user with the administrator role. I've created a pre_get_posts action, and trying to use $query->set to filter the posts only in ...
0
votes
1answer
25 views

Why is pre_get_posts hook invoked multiple times?

I am using pre_get_posts hook, however as it is not working as expected, I have discovered that the function is being invoked 4 times, and while the first call is running as expected, subsequent calls ...
0
votes
1answer
43 views

Modify author archive query to combine two queries

I'm working on a site that allows multiple authors on posts by using an "additional authors" Posts to Posts connection type. Posts still have a standard author, but they can optionally have ...
0
votes
2answers
68 views

is_category in pre_get_posts results in php notices

I'm trying to set posts_per_page depending on what category is being shown. The following code works. function category_posts_per_page( $query ) { if ( $query->is_main_query() && $...
0
votes
0answers
29 views

Modify a query with no results in pre_get_posts

I'm looking for a way to ensure a query always returns posts, even if the original query was empty. The query is a custom post type archive query, and my goal is to change a query var if the query has ...
0
votes
0answers
38 views

Why does this incorrect pre_get_posts meta_query work for sort order?

I've got a fairly complicated post type archive page that I need to order by two meta fields and the post title. To do that, I'm using pre_get_posts with a meta_query that simply tests for the ...
0
votes
2answers
36 views

List all posts associated under custom taxonomy

I have created a custom post type. Within that I have assigned two custom taxonomies. One (computer) is what I use to drive the template files and slug of page (taxonomy-computer.php). The second (faq-...
0
votes
0answers
20 views

Sorting meta_query of custom post type

I have 2 custom post types Transit and Colors, connected with a relationship - as a transit post has a custom field to select a color post that it will belong, that stores the Color post ID. I ...
0
votes
0answers
74 views

WooCommerce pre_get_posts meta_query no results message

I have implemented a product dimensions filtering system, which appears to "mostly" work. I pass a min & max set of dimensions for the width and/or height of the products via query string. Then, ...
0
votes
1answer
26 views

How order by works?

I'm trying order some posts by meta_value (featured) and then by title. I'm getting the featured ones in a correct order, however, I got some non-featured mixed after the featured ones, I noticed that ...
0
votes
0answers
25 views

All sticky posts are displaying on home.php and ignoring tax_query

I'm trying to query the main loop on my home.php template to display posts which are only in a specific taxonomy 'issue-41'. I'm using the pre_get_posts function to achieve this but when testing it, ...
0
votes
0answers
94 views

Tax query in pre_get_posts not working

I'm trying to filter the main query on the home.php template so I only shows posts from the latest issue taxonomy. What I have so far is the below but it is still showing all the posts on the ...
4
votes
1answer
371 views

pre_get_posts with tax_query causes empty result

I need to adjust the main loop for all archive pages in the following way. I have registered a custom taxonomy "myposttypes". Every single post is assigned to exactly one term of "myposttypes". If it ...
1
vote
1answer
24 views

Include Site 1 Posts in Query for Sub Sites

So I have a Primary site that posts announcements about the sub sites, for each of those posts I have a meta value that the user sets for which site those announcements apply too. On each of those sub ...
-1
votes
1answer
21 views

Trying to add function with pre_get_posts for plugin search [closed]

Keep getting syntax error, not sure why. This is the code I'm using function my_epl_listing_default_sort($query) { // Do nothing if in dashboard or not an archive page if ( is_admin() || ! $...
0
votes
1answer
373 views

pre_get_posts with WooCommerce Shortcode Query

I want to add some additional query vars and customise the query for woocommerce [products] shortcode. i'm trying to add some additional fields and conditions with pre_get_posts and posts_fields hooks ...
1
vote
1answer
91 views

Add custom field (value) to search result (without plugin)

I need to add custom field to wordpress search result. This what i tried to do is used pre_get_posts filter, like this: function search_filter( $query ) { $key_fields = array ( 'buty' ); $...
1
vote
0answers
90 views

Why the pagination for a query modified by pre_get_posts doesn't mind the number & order of posts sorted & selected based on a custom date time field

I have the following task I'm trying to accomplish. I want to display events in the post post type. I want to sort them by date and time and there should be events only displayed happening right now ...
0
votes
1answer
266 views

Woocomerce - Order products by float attribute in archive pages

I'm trying to order the products in the frontend by a float attribute (pa_od), I have this code but the order is incorrect add_action( 'woocommerce_product_query', 'imcar_custom_product_query' ); ...
0
votes
1answer
233 views

Order posts by title and custom field value?

I want to order posts by title, but always show featured posts first. I have the code below to order posts by title. Now I would like to always show posts first that have the metafield '...
1
vote
0answers
19 views

hide custom post types with specific meta key ON admin backend

I got a tricky problem I need to solve. So say, you have a custom post type of event, then this event has a scheduled event date by a custom field. And this event is recurring, like every Saturday ...
0
votes
0answers
30 views

Custom Order Current Query: By Meta Key and Category

I recently posted a similar question, but it didn't seem anyone could help - I've thought of a simpler method below if anyone can offer any advice? Been trying for 3 days on this with no luck, am sure ...
0
votes
1answer
612 views

Filter WooCommerce archive pages by an additional category

I'm working on a WooCommerce bookstore site with a custom taxonomy.php template that generates different archive pages based on a category called "Highlights". So, for example, there is a "Best ...
1
vote
1answer
26 views

pre_get_posts dont firing… Anybody knows whats the wrong with my code?

I trying exclude category "1" with pre_get_posts: function exclude_category( $query ) { if( !is_admin() && $query->is_home() && $query->is_front_page() && $query->...
2
votes
2answers
51 views

Searching through different categories on different pages code is not working

I am adding a default WordPress Search widget through Elementor on two of my page, page X and page Y. Page X ID = 100, page Y ID = 200. I want the user to be able to search through category 37 when he ...
1
vote
2answers
44 views

Search function works improperly

So I am trying to edit my Search so it searches only for specific categories. I am adding this code to my functions.php file in child theme: function searchcategory($query) { if ($query->...
1
vote
1answer
99 views

WP_Query ignoring tax_query when is_singular

I’m trying to set up some post permissions using taxonomies and I'm applying my rules in a pre_get_posts action. It’s working fine when multiple posts are found, but in class-wp-query.php near line ...
0
votes
1answer
167 views

Multiple meta_key ordering with pre_get_posts

I'm trying to order products by multiple meta keys, so that the most selled products appear first and the out of stock products lastly. I managed to make the out of stock products show in the end, ...
1
vote
1answer
153 views

Modify query in pre_get_posts action is messing up my nav menu

I am modifying the query for custom archive pages. I am using the pre_get_posts action. I need to catch some GET variables to modify my query. It is working great on the archive page for one custom ...