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
3
votes
1answer
586 views

Pre get posts for single post

I pre get posts for a events archive page: http://www.netzwerk-leipziger-freiheit.de/veranstaltungen/ function my_pre_get_posts( $query ) { // do not modify queries in the admin if( is_admin() ){...
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 ...
1
vote
1answer
458 views

Alter query using pre_get_posts() if meta_key is present

I am trying to modify the number of posts per page for archive pages that contain category sticky posts (added by the Category Sticky post plugin) to do so I am using the pre_get_postshook and have ...
0
votes
0answers
16 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,...
1
vote
1answer
437 views

Filtering out child category posts from parent category archive not working

In this example cat 5 is the parent category, and 42 is a sub-category. I the main category 5 archive I don't want any posts from sub-category 42 displaying. Why isn't this working? function ...
18
votes
4answers
11k views

Modify Taxonomy pages to exclude items in child taxonomies

I found this question: Theres a way to use $query->set('tax_query' in pre_get_posts filter? which seems to indicate that yes, you can alter the taxonomy query on taxonomy archives via ...
1
vote
1answer
535 views

pre_get_posts for two loops on same page

I'm using two loops on homepage, one is to show the 3 latest posts, and the other one to show the rest of the posts minus the 3 first posts. I'm using the following code for the pre_get_posts. ...
3
votes
2answers
518 views

How to target the default Recent Posts and Recent Comments widgets with pre_get_posts?

I added the following to my functions.php: add_action('pre_get_posts', 'keyl_get_emp_posts'); function keyl_get_emp_posts($query) { if ($query->is_main_query()) $query->set('...
1
vote
1answer
773 views

Search by type posts and taxonomy

I want that my search page give results of posts (that i want that show) and taxonomy The code to get just two type of posts: function searchfilter($query) { if ($query->is_search &&...
0
votes
2answers
700 views

pre_get_posts gives 404 error on Custom Post

I know this has been covered in different ways a thousand times (I read all the posts), but it seems not specifically for my case. I can't really get it to work myself, being a big development noobie. ...
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' ) || ...
37
votes
12answers
25k views

Sort on meta value but include posts that don't have one

I've been modifying the built in WP search using the pre_get_posts filter, allowing the user to sort the posts (including a bunch of custom post types) by different fields. The problem I'm having ...
0
votes
1answer
481 views

Hooking get_pages()

On a site this is a plugin that uses get_pages() to make a list of pages. The list is used to create a list of pages and create next page and previous page links. ( http://wordpress.org/plugins/next-...
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
1answer
704 views

Apply pre_get_posts to specific custom post type in the admin area

I've created a few CPTs, and wish to have one of the custom post types to query from other post types. So what I did is I used the codes below to alter the query: add_filter('pre_get_posts', '...
0
votes
1answer
547 views

Order posts by custom column using pre_get_posts

Is it possible to sort posts by custom column in wp_posts table (in this case, 'fav_count' column) using pre_get_posts filter? If not, is there any other way to do this before the main query is run, ...
1
vote
1answer
601 views

I am trying to hide a custom post type category to logged in users with Pre_Get_Posts

I have a custom post type in my theme called portfolio. It's categories are called project-categories. I am trying to exclude a project category for logged in users. I cannot get this to work at all. ...
0
votes
2answers
701 views

pre_get_post filter returns results when there should not be

I am trying to filter a CPT by an array of post ids using pre_get_post filter within my plugin. Everything works fine when there are post that match the ids, but when there are no post that match the ...
0
votes
1answer
649 views

using posts_where for meta data on pre_get_posts

Let's say you have some code which filters posts by using pre_get_posts e.g. add_action( 'pre_get_posts', 'be_exclude_category_from_blog' ); function be_exclude_category_from_blog( $query ) { ...
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', ...
1
vote
1answer
683 views

Only show certain post types in recent posts widget

I have a custom post type which has been made to show in archives using the code below in my theme's functions.php : add_filter('pre_get_posts', 'query_post_type'); function query_post_type($query) { ...
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 ...
2
votes
1answer
1k views

How do I sort posts by custom taxonomy?

I am trying to sort posts by custom taxonomy. I have a custom post type which is course-cpt and I have created custom taxonomies within it, I need to sort the courses by course-level (which is the ...
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 ...
1
vote
1answer
447 views

Weird problem with pre_get_posts and $query->is_page()

The following bit of code was used in my fictitious plugin to redirect the non-logged in users from page 173 (ID) to sample-page (slug). The code's working good. But just today, I figured out that the ...
1
vote
1answer
406 views

Sanitize Custom Field Value in Meta Query

I'm using the pre_get_posts hook with meta_query args to filter an archive loop by dates. Basically, there is a custom post type of "events", and my users type in a date as a custom field for the ...
0
votes
1answer
29 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
1answer
790 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 ...
4
votes
2answers
841 views

WP_Post_List_Table::get_views - Have post counts account for filters?

Toward the top of the edit.php screens there is a list that displays post statuses along with the post count. I believe this is generated by WP_Post_List_Table::get_views. For example All (7) | ...
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
47 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 ...
2
votes
2answers
330 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
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
152 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
23 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'] ) && !...
5
votes
1answer
492 views

Order Search Results Page by meta_value If no Value Return Remaining Results

I am trying to order the search results page of my site by a specific meta_key. The catch is I need to return all results even when there's no value in the meta_key The posts are custom post types ...
1
vote
1answer
261 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
51 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
105 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
40 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
114 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
113 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 ...