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
1answer
12 views

pre_get_posts and search query for admin

I'm going round in circles with trying to include several custom fields in my search results. The custom fields are managed via ACF. At first I want this for the admin. /wp-admin/edit.php?post_type=...
1
vote
1answer
720 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) { ...
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 ...
0
votes
0answers
10 views

Alphabetical taxonomies and pre_get_posts not working

I am currently building a website that makes use of a theme. The theme has a custom taxonomy called team_group. I created a custom template called taxonomy-team_group.php to be able to list the posts ...
0
votes
2answers
28 views

order posts by a secondary query that counts items

i'm working on a music plugin. It uses a custom table tracklist_items to store every tracks of a playlist; which has a structure like this : |id| |track_id| |tracklist_id| |order| |time| So if I ...
0
votes
1answer
24 views

Problem ID to exclude specific posts from category

I'm trying to exclude specific posts from category/archive with these functions: function exclude_single_posts_cat($query) { if ( is_admin() || ! $query->is_main_query() ) return; ...
4
votes
1answer
59 views

How to exclude latest x posts from a paginated query?

I have a WordPress loop, that is part of an AJAX call. Every time a button is clicked, this function is called, runs the below loop and outputs the next 6 posts. There are already five posts shown by ...
21
votes
3answers
7k views

Sticky Posts exceed posts per page limit

I'm using pre_get_posts to adjust the number of posts displayed on my homepage. function lifelounge_query_adjust( $query ) { if ( is_home() ) { set_query_var( 'posts_per_page', 12 ); ...
4
votes
1answer
774 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() ){...
0
votes
0answers
12 views

how to exclude specific pages form wordpress search results?

I want to show only today's events and the next events in Worpdress search results for every post with category "events". This is my code and it doesn't work, the events that have ended still appear ...
1
vote
1answer
482 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
1answer
29 views

How to use pre_get_posts

I'm a little stuck on how this hook is used. Does it go in the search template, the archive page, or in functions? Does it surround the pagination code? Also, does it work differently when I'm ...
0
votes
1answer
488 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
1answer
482 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 ...
2
votes
1answer
549 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 ...
0
votes
0answers
51 views

pre_get_posts causings DB error when using ( 'posts_per_page', -1)?

Has anyone encountered a DB error when using pre_get_posts to display ALL posts on a CPT archive page? This error... WordPress database error: [You have an error in your SQL syntax; check the ...
1
vote
1answer
620 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
561 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
1k 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 &&...
-1
votes
1answer
44 views

WP_query sort by custom meta_key “price”

I've created a custom post field for use on my site that is the price of a real estate item for sale (that is the subject of the post). It should sort posts archives by price but I cannot get it to ...
0
votes
2answers
852 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
497 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
28 views

cannot filter after using pre_get_posts

I am using pre_get_posts() in the backend for a custom post type screen where all the records are usually displayed. The action I have is showing only posts for today's date. This is working fine ...
0
votes
1answer
893 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', '...
1
vote
2answers
382 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 ...
0
votes
1answer
574 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
642 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
28 views

How to change the amount of posts previewed on a page?

I have tried this code, but it doesn't work. function university_adjust_queries($query) { $query->set('post_per_page', '1'); } add_action('pre_get_posts', 'university_adjust_queries');
0
votes
1answer
701 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 ) { ...
18
votes
5answers
12k 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 ...
0
votes
1answer
1k 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
84 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
18 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
25 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' ) || ...
38
votes
12answers
27k 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
20 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
76 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
2answers
749 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
0answers
17 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
24 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
17 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
95 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
436 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
41 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 ...
4
votes
2answers
863 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
27 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
87 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
17 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
90 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. $...

1
2 3 4 5
9