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
7 views

Using pre_get_posts filter to reset main query

How can I use pre_get_posts filter to reset main query? I want to use the filter in a page and reset the page query data and make a new query to display the latest posts.
0
votes
0answers
16 views

Order column custom date using pre_get_posts

How I want to sort my custom column contain string date with format j F Y my column data contains string example "21 januari 2021", how I can change the format to date so I sort it. My value ...
0
votes
0answers
9 views

Arranging products in Custom Taxonomy Archive by Category with new WP_Query

I'm using a shortcode and thus using a new Wp_Query - but If I could do it with pre_get_posts in a shortcode I'd also be happy. On a custom taxonomy page "range" I need to group products by ...
0
votes
1answer
9 views

How to display remaining post ( in post__in ) if posts are less then post per page?

I am sorting product on product category page. I want to display sale product at first and show remaining product after it. I am using pre_get_posts hook to modify query. My CODE function modify_query(...
0
votes
0answers
23 views

Changing posts per page on home page breaks pagination on home only

I'm pretty much a beginner here and have a bug in my website. I have a grid layout of 4 x 4 with a static post as my first one on the home page. It displays on the homepage only using <?php if(...
0
votes
0answers
10 views

How to remove scheduled posts from all posts in admin?

How can I remove scheduled posts from all posts list in admin? I am always scheduling posts in the future and if they are many shown on all posts it is a mess to work with. The URL for it is BOTH ...
0
votes
0answers
27 views

meta_query logic with query

I'm currently building a pre_get_posts function that will do the following: Filter out from the filters that I have set up but also I need some date filtering logic, for example if I have a project ...
0
votes
0answers
17 views

Display posts between two dates meta_query wordpress

I have two custom fields (advanced custom fields) of "start_date" and "end_date", I want to filter the projects that are between the two given dates, here is what i've tried: ...
0
votes
0answers
34 views

building filtering system

I have a custom 'pre_get_posts' function, that attaches meta_query: function my_pre_get_posts($query) { $query_filters = array( 'color' => 'color', 'size' => 'size', 'type' ...
0
votes
0answers
8 views

How to easily exclude first post in pre_get_posts only in queries other than page 1?

In home/category pages (blog), I need to have 4 posts in first page, then 3 posts in all other pages. Problem is, I cannot simply alter posts_per_page, because in following pages I would have wrong ...
-1
votes
2answers
62 views

How to ignore posts in pre_get_posts?

I'm using pre_get_posts and looking for a way to exclude posts in the main query. I'm using query_vars to query posts from a specific category and looking for a solution to exclude those posts in the ...
0
votes
1answer
18 views

custom sorting media with multiple filters - error: not unique table/alias: wp_postmeta

I tried to add a column "Downloads" in the media library and make it sortable. This all works fine, but as soon as I search for a filename next to the sorting, the following error occurs: ...
0
votes
0answers
10 views

Including others post width pre_get_posts and meta_query on archive page tag

I have a problem with a pre_get_pots function for the archive page tag (is_tag()). I want to add some posts to diplay on the archive page tag, with meta_query, corresponding to a meta field (of post). ...
0
votes
0answers
13 views

pre_get_posts WP_User_Query filter

I’m trying to run a custom user search, which filters via an array set in ACF. Essentially, I’m storing a list of users (contacts) in an array, and, if that is set, I want to search through those ...
0
votes
0answers
25 views

meta_query is overriding default search

I'm using default search with get_search_form() function. I want to search also by custom fields, so I wrote a hook: function search_products($query) { if (is_search() && $query->...
0
votes
0answers
34 views

Custom posts per page on first page generates wrong number of pagination links

I've looked at every solution I could find, but everything I've tried seem to have this same issue (although nobody has mentioned it). I have a custom taxonomy with 30 posts in it. Global ppp is 9, ...
0
votes
0answers
19 views

Filter search results by post_tag if exist

I want to get posts from search results using post_tag. for example If a user input in the search form "party". First, we search if "party" exists in post_tag and show all posts ...
0
votes
0answers
30 views

Random order not working correctly when using default loop + $query->set

I'm trying to use orderby => rand on a custom taxonomy page, but it's not working correctly. On other pages I use below piece of code to show my posts from a custom post type: $paged = ( ...
0
votes
0answers
13 views

set orderby to main query using pre_get_posts hook

I am using pre_get_post to alter the orderby of the query and I am not getting the expected result. This is my code: function dl_pre_get_posts( $query ) { if ( is_admin() || ! $query->is_main_query(...
0
votes
0answers
15 views

post count not reflecting pre_get_posts action

please can someone help me out! I'm using a plugin to create a widget for a custom post type archive which is showing in a sidebar. I then added a function to further filter the results using ...
0
votes
0answers
46 views

How do I troubleshoot pre_get_posts with Debug Bar?

I am using pre_get_posts() on a custom post type Archive page, to change the sort order. When I view the query using Debug Bar and WP Query Monitor, I can see that my action is indeed firing. If I ...
0
votes
1answer
53 views

pre_user_query vs pre_get_posts

I've used pre_user_query to edit the actual sql query and it works wonderfully. Now I'm trying to work with pre_get_posts and its not at all the same thing. I need to do an INNER JOIN.. In this ...
1
vote
0answers
18 views

How to sort custom columns for custom post type without meta query and pre_get_posts?

I have a custom post type orders and multiple custom columns. I've added the custom columns with the manage_orders_posts_columns hook and registered the columns as sortable with this hook: manage_edit-...
0
votes
2answers
29 views

Can I alter the main loop to 'orderby' a custom callback?

I have a custom post type where the title is a name, and I want to sort the loop by the last word in title, the surname. I can easily break the names apart and filter by the last word in a custom ...
1
vote
0answers
22 views

Sort the result of the main Query in shop page by a custom taxonomy

In a WooCommerce site, we have products that have a custom taxonomy that are assign to a region. When the user visits the site, we activate the browser geolocation, and we assign the user a region in ...
0
votes
0answers
21 views

query_posts in pre_get_posts removes all body classes - how can I reset or keep them?

I've created a function that searches images for a tag, uses them to create a loop and build an array of their parents' IDs. Its in a pre_get_posts. Its all working fine except I'm obviously ...
1
vote
1answer
62 views

Change archive page template using pre_get_post

I've used pre_get_post to modify and display my custom post type in an archive page with pagination which uses my custom created archive template, see here. It it displaying the posts exactly what I ...
0
votes
0answers
12 views

Multiple meta_key ordering with numbres

I am trying to create a search with different search terms and it works correctly for me. The problem is that these fields are a string and not a number, so it does not order me correctly (for example,...
1
vote
1answer
23 views

Replace/Mute/Stop Search Query

I'm trying to replace the search functionality in WP. I've already created the search.php template with all the results I want to display. I don't want any results from the WP database. I just want ...
0
votes
1answer
18 views

Setting user permissions per post

I am trying to assign privileges on posts on a per post basis. Essentially, I have a custom post type which have authors. They can edit their own posts. I then have another role, which should be ...
0
votes
0answers
14 views

WP_Query orderby meta_value Speed

I currently sorting my posts using this code: function main_query($query) { if ( is_search() || is_home() || is_front_page() || is_archive() ) { if (!is_admin() && $query->...
0
votes
0answers
24 views

Show only posts without format in admin screen

The situation In admin screen (edit.php?post_type=post) using format selection dropdown adds a URL parameter like post_format=video. This makes the query to only look for posts with post-format-...
0
votes
1answer
149 views

How to exclude woocommerece product category in search results?

I'm looking for a way to exclude a product category from search results with Woocommerce using the pre_get_posts action and $query->set(). I am searching using the Wordpress ?s=keyword search. I know ...
0
votes
1answer
18 views

Prevent A Specific Custom Post Type Showing In WP Search Results Page

I've excluded certain pages from the WP Search with the code below in my functions.php file. I also need to exclude a custom post type called 'sites'. I can't seem to find the custom post type ...
0
votes
1answer
25 views

Can a link in WordPress contain a query string that is picked up as $_POST

This question is about a very specific circumstance. The site has one template file that can run a default category query with thumbnail, linked title and excerpt, or it can run a linked title only ...
0
votes
2answers
323 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=...
0
votes
0answers
13 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
33 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
26 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
184 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 ...
0
votes
0answers
15 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 ...
0
votes
1answer
60 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
0answers
55 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 manual ...
-1
votes
1answer
238 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
1answer
34 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
2answers
31 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
70 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
21 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
109 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()) { ...
1
vote
0answers
104 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
2 3 4 5
10