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.

-1
votes
0answers
15 views

Showing all posts in specific term except the first 10

My client wanted to display all posts except the first 10 in one of the term in a custom post. Additionally, the pagination between the pages should work correctly. I know I need to use pre_get_posts, ...
0
votes
0answers
22 views

Display Specific Posts at Start of Loop

I'm attempting to mimic 'sticky' functionality on a custom post type using a simple ACF true/false field. I'd like to use pre_get_posts to modify my query to first show my 'sticky' posts at the top ...
1
vote
0answers
22 views

Blog page pagination is not working after using the offset argument

I have displayed the recent 7 posts then shown the remaining posts. So I have added the offset = 7 in the argument to skip above 7 recent posts and display remaining posts but the pagination not ...
0
votes
1answer
18 views

$query->is_main_query() is causing query's tax_query to be ignored

I was having issues when modifying the main query hooking onto pre_get_posts as it was causing the menu to not be displayed. This the original code: else if ( isset( $_GET['category'] ) && !...
2
votes
2answers
126 views

Meta query interfering with orderby relevance

I'm trying to re-order the search results using the pre_get_posts hook and set the orderby parameter to relevance. I also want to put priority posts, which is based on post meta _prioritize_s to be ...
0
votes
1answer
33 views

Meta Query “IN” doesn't work with ACF checkbox filter

I try to integrate to my CPT archive page some ACF filters. I start with this tutorial : https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/ It works when I use it ...
0
votes
0answers
25 views

Duplicate posts when using pre_get_posts for author.php

I'm trying to change the main query into the author.php but there are some duplicate posts?! How can I fix it, Thank you! function change_author_query( $query ) { if ( $query->is_author() &...
0
votes
0answers
24 views

How do I search WordPress posts by categories and tags in addition to title and content?

I would like to expand my site's default search fields (title and content) to include categories and tags without installing a plugin. Including the below code in functions.php allows me to ...
1
vote
1answer
30 views

My Main Query Modification is Messing up my dynamic main - why?

This has been very difficult to track and I have enabled only the plugins I need (ACF and my own plugins that deal with content type creation). I noticed that on some pages, my menus were gone, this ...
0
votes
1answer
51 views

Woocommerce - exclude only older out-of-stock items

Trying to find a way to exclude out of stock items from listing, but only after some time (let's say, if they're older than one month). I know I can exclude all of them easily, or change the order ...
0
votes
1answer
22 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
45 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
12 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
27 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
33 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
27 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
43 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
80 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
52 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
24 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
177 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
29 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
17 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
39 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
26 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
51 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
27 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
26 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
116 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
31 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
48 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
80 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
34 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
38 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
21 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
94 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
26 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, ...
1
vote
0answers
106 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
453 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
27 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
425 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
114 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
93 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
326 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' ); ...