I found this post about how to change a specific query but can't find a way to do the following: I have several queries on homepage retrieving posts from categories, posts from "New"s, posts from "Recipes", posts from "Promo" etc I have a slider area at the top that display posts from all categories that uses the tag "featured" How can I prevent all other queries on home from retrieving posts using "featured" tag so posts using the tag do …
In wordpress I made a simple plugin that adds a function to the pre_get_posts hook. I need to be able to search products by their variation sku. The query seems to work as when I var_dump the posts returned by the WP_Query they actually include the correct posts(products). But the search result page doesn't show them and display "No products were found matching your selection." no matter what theme I use. The code I tied: function awesome_plugin_init() { function is_woocommerce_really_activated() …
I’m trying to apply the pre_get_posts to a specific core/query block, in order to programatically modify the number of posts displayed when a custom attribute is set. If I use the render_block hook, the block is already pre-rendered and so modifying the query using pre_get_posts has no effect. Is there any way to achieve this? (I don't need help adding the custom attribute; I've done so already using block filters.)
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 though is that when I tell WP to sort by a meta value it will exclude all posts that don't have that meta value set. This causes the number of results to change if you change sorting from say "Price" to "Date" because "Posts" don't have "Price" …
Running pre_get_posts, I want to order the items by a custom meta value. The problem is that by default this doesn't exist. Only if a user performs a certain action does the meta key/value get added. That being said, even if they don't ever click on the item itself, I would like it to be displayed. Basically the order is the number of clicks an items has received. So, the most clicked item should be first and the items that …
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 tried the following : add_action( 'pre_get_posts', 'my_set_category_posts_per_page' ); function my_set_category_posts_per_page( $query ) { global $wp_the_query; if ( 'category_sticky_post' === $query->get( 'meta_key' ) && ! is_paged ) { $query->set( 'posts_per_page', 8 ); } return $query; } I var_dump($query) I do get [meta_key] => category_sticky_post in …
Update: This question isn't really relevant anymore. I switched some code, so now the filtering works. Looks like the query wasn't working, because I triggered the indexTaxonomyCounts function from a template? But I now have another problem, but will write a different question for that. Original question: I want to filter my custom products by some taxonomy and meta filters. This works for regular queries, but not for my search results. At least, the filtering works, but getting the right …
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 ids I am getting all the post within the CPT that I am trying to filter in return. What I need id there are not post ids found (or post from the post ids) is the archive page to display the …
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('post_type', 'employee'); } and so far it's effectively filtering out the search results. The default widgets Recent Posts and Recent Comments aren't budging, though. What gives?
Possible Duplicate: How to know which one is the main query? I'm curious to know what is the so called "main query"? What I have is two queries on front page. if (have_posts()) : while (have_posts()) : the_post(); // do the main loop endwhile; endif; $posts = new WP_Query(array('post_type' => 'some_other_post_type')); while ($posts->have_posts()) : $posts->the_post(); // do the secondary loop // but still operating with the some_post_type endwhile; wp_reset_postdata(); And what I want is just to modify the main query …
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 pages I have set up, where each shows all posts from "venue-one" or "venue-two", pagination is not working past page 3. I am using numbered pagination and visually it does display what the correct number of pages should be (given what I set posts …
I've been pulling my hair over this strange issue for several hours now and need some help figuring out what's going on. Allow me to explain my setup followed by the behavior and question. Setup: I've set my 'posts page' to domain.com/search/ where I intend to list all my posts. Since I need a custom layout for displaying my posts, I've the following redirect in 'template_include' hook: /*Use custom template to render posts on /search/ */ if ( is_home() ) …
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 'wiloke_listgo_toggle_highlight' with the value '1'. How can I accomplish this? /* Order Posts Alphabetically */ function prefix_modify_query_order( $query ) { if ( is_main_query() ) { $args = array( 'title' => 'ASC' ); $query->set( 'orderby', $args ); } } add_action( 'pre_get_posts', 'prefix_modify_query_order' );
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. function tax_and_offset_homepage( $query ) { if ( !is_admin() && $query->is_home() && $query->is_main_query() ) $query->set( 'post_type', array( 'post') ); $ppp = get_option( 'posts_per_page' ); //$ppp = 300; $offset = 3; if ( !$query->is_paged() ) { $query->set( 'posts_per_page', $ppp); $query->set( 'offset', $offset); } } …
Using the below code, i have excluded the taxonomy (slug:expired) from archive or tag or taxonomies pages in front-end. add_action('pre_get_posts','custom_get_posts'); function custom_get_posts($query) { if ( !is_admin() && $query->is_main_query() ) { $taxquery = array( array( 'taxonomy' => 'current-status', 'field' => 'slug', 'terms' => array( 'expired' ), 'operator' => 'NOT IN' ) ); if( $query->is_post_type_archive( 'job' ) || $query->is_tag() || $query->is_tax() || $query->is_search() || $query->is_term() || $query->is_category() ){ $query->set('tax_query', $taxquery ); } } } The function excluded all the posts tagged to …
I am trying to query a custom post type based on what a user inputs into a form, I am having trouble querying multiple taxonomies in pre_get_posts filter, I can't seem to find a way to set the query more than one taxonomy function custom_search( $query ) { if ( is_archive('experts') && $query->is_main_query() && !is_admin() ) { $keyword = get_query_var( 'keyword', FALSE ); $industry_select = strtolower(str_replace(' ', '-', get_query_var( 'industry_select', FALSE ) )); $speciality_select = strtolower(str_replace(' ', '-', get_query_var( 'speciality', …
I'm trying to include more than one term_id(multiple checkboxes filter) on a single category page. I managed to recollect enough to build a tax_query with pre_get_posts, but now it seems, I have two tax_queries, one is in WP_Query->query_vars and the other is just in WP_Query(that one is of WP_Tax_Query type): object(WP_Query)#1968 (50) { ["query"]=> array(1) { ["product_cat"]=> string(77) "parent-category-slug/slug-of-the-category" } ["query_vars"]=> array(59) { ["product_cat"]=> string(56) "slug-of-the-category" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" …
I'm trying to use pre_get_posts to modify my post display request. The posts I want to display are custom post types. I am in a custom page template. I have this is my functions.php function tax_and_offset_homepage( $query ) { if ( !is_admin() && $query->is_post_type_archive( array( 'project' ) ) ) { $query->set( 'post_type', 'project' ) ; if (null!==(get_option('sticky_posts'))){ $count_sticky = count(get_option('sticky_posts')); } $ppp = get_option( 'posts_per_page' ); $offset = $count_sticky; if ( !$query->is_paged() ) { $query->set( 'posts_per_page', $ppp - $offset ); …
I have a search form with two dropdowns showing custom terms and two text fields for custom meta keys, The search work with tax_query but meta_query has no result. Here is the pre_get_posts hooks callback function. Why didn't get results through meta keys filters? function sm_pre_get_posts( $query ) { // check if the user is requesting an admin page // or current query is not the main query if ( is_admin() || ! $query->is_main_query() ){ return; } // edit the …