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

Query not ordering items correctly due to non existent values

I am trying to pre_get_posts and set a custom orderby. From searching through other Stackoverflow, WordPress Development questions and answers I managed to build this so far. This works 99% correctly,...
0
votes
1answer
17 views

How to hide private posts even if user is admin

Even if admins, I dont want to view private posts for custom post type. Added the following code. function my_function( $query ) { if ( ! is_admin() && ( is_singular( 'my_post_type' ) || ...
0
votes
1answer
16 views

How can I re-query post_type and rewrite the url?

I have the url slush like the below. /category/land-residential-farm/?property_com_listing_type=sale I want to write the url to /category/land-residential-farm/property-for-sale then re-query the ...
0
votes
2answers
58 views

Wordpress meta query not working

add_action( 'pre_get_posts' , 'my_pre_get_posts' ); Function my_pre_get_posts( $query ) { $value= $_GET['s']; if( $query->is_main_query() && $query->is_search()) { ...
0
votes
0answers
13 views

Search in multiple categories using $query->set

Right now i have this code: function exclude_categorii( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_tax ) { $query->set( 'category__in', ...
0
votes
0answers
20 views

Empty posts return an HTML comment

I'm using the script from this answer to exclude empty posts from the main query. It work well, but since the new WordPress editor, some empty posts return a comment rather than an empty string. i....
0
votes
0answers
15 views

Stumped on filter during pre_get_post for category

I'm attempting to create a filter on a posts archive page. Currently have filters set up for author, category and year and using pre_get_posts to use the same page. Both the author and year filters ...
1
vote
0answers
90 views

Category param redirect to post type archive

I'm trying to filter categories by post type. e.g: the following URI will retrieve all post from the category news. https://example.com/category/news/?post_type=post. Here is how I'm updating the ...
0
votes
1answer
30 views

How to show only specific category post by user role without plugin and restrict all other cats

I am using basic post for adding posts. 5 categories exist ( cat_A, cat_B, cat_C, cat_D, cat_E). And I have 2 custom user_role for registered visitors that is 'viewer_a' and 'viewer_b'. I need to show ...
0
votes
0answers
23 views

Apply pre_get_posts filter in a certain moment (just for a particular block of posts)

I want to apply a filter to a block of posts, for a custom field that I created, called: 'highlighted_in'. To filter the results I'm using the 'pre_get_posts' hook, and it works. But I think my ...
0
votes
0answers
16 views

Search filter inside class

I have a theme class In public function search exclude how to pass arguments? public function searchFilter( ){ function searchFiler($query, $array_ids(10,17){ $query->set(‘post__not_in’, $...
0
votes
0answers
23 views

sort posts on front end using url query

I am trying to sort posts on my archive page based off a custom field. The custom field is a number. I have the following in my functions.php file - function wpse139657_orderby(){ if( isset($_GET[...
0
votes
0answers
15 views

Security in Wordpress, better to edit pre_gets_post or protect single pages?

I am making a website where user must register and when they are logged in they must complete the profile informations with other infos before they can access the content. I wrote this function ...
0
votes
1answer
49 views

Compare meta key to current date in pre get post

I'm looking for a way to compare a string to the current time. I found this question "pre_get_posts filter using numeric meta_query comparison (from dates)" and I'm confused as to get it to work. $...
0
votes
0answers
22 views

Adding post_type filter on pre_get_posts gives 404

I have a basic WordPress query: $the_query = new WP_Query(array( 'post_type' => array('post'), 'posts_per_page' => -1, 'ignore_sticky_posts' => true )); But if I try ...
0
votes
0answers
26 views

PaidMembershipsPro plugin's query filter not working for REST API calls

I have a custom REST API in WordPress that returns a list of posts using the get_posts function. I am using the PaidMembershipPro plugin, which typically filters the get_posts results according to ...
0
votes
0answers
32 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
154 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
24 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
342 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 ...
1
vote
1answer
266 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
53 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
109 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
41 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
115 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
58 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
115 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
13 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
20 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
33 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
34 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
42 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
169 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 ...
3
votes
1answer
324 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
64 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
11 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
70 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 ...
3
votes
2answers
396 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....
1
vote
1answer
45 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
22 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
120 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
30 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
188 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
55 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
28 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
385 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
82 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
64 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
173 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() && $...