I am using WooCommerce on my Wordpress site. My homepage only has one category so the search would only search in that category (client wants the homepage to only have one category). Whenever I try to search for something from a different category, it simply says that product is nonexistent.
How can I change the location where it should search so I can make another page with all the products?
To alter the search query for only the home page to display products from a specific category, use the pre_get_posts hook
function include_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set('tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array("**term name here**") ) // replace **term name here** with the slug of the term you wish to show on the home page
));
}
}
add_action( 'pre_get_posts', 'include_category' );
Depending whether you've set a static page or latest blog posts page as your home page you'll need to change $query->is_home() with $query->is_front_page()
Related
I need to alter the search behavior of a WordPress site where the theme doesn't use searchform.php at all (it's the Divi theme). I'd rather not employ a plugin to do this as the site has many plugins already.
The default state of the page is to show "all posts from all categories" (/?cat=0) with a category list dropdown generated using wp_dropdown_categories() that allows the user to select other categories.
The desired use case is:
The user selects a category from a dropdown
The browser goes to the selected category's archive
The user types a search query into the search box on the category archive with the intent of seeing only posts and pages in that category that contain the search term.
I figured I could use the pre_get_posts hook to do this. So I did the following:
function search_by_cat()
{
global $wp_query;
if ( is_search() ) {
$currenturl = add_query_arg( $wp->query_vars, home_url( $wp->request ) );
$previousurl = wp_get_referer();
if ( strpos( $previousurl, '/category/' ) !==false ) {
$searchcategoryurl = untrailingslashit( $previousurl ) . $currenturl;
wp_redirect( $searchcategoryurl );
exit();
}
}
}
add_action('pre_get_posts', 'search_by_cat');
But this just results in ERR_TOO_MANY_REDIRECTS in the browser. I think it might be because the user is submitting a search that results in the same destination as the referrer. How do I break out of the redirect loop so I can (essentially) redisplay the page of results? Or is my entire approach all wrong? Is there a better way?
Here's the general idea from my comments, you'll need to change the CPT and taxonomy types to match your code. Tax queries are a little weird because of the nested array syntax. The comments should hopefully help understand it more.
add_action(
'pre_get_posts',
static function (WP_Query $query) {
// We only want to run on the main search query
if (!$query->is_search() || !$query->is_main_query()) {
return;
}
// Attempt to get the parameter, or default to 0
$category_id = (int)($_GET['category_id'] ?? 0);
if (!$category_id) {
return;
}
// Since we're search by a taxonomy, we probably want to bind it to a type, too
$query
->set(
'post_type',
// Change as needed
'events'
);
$query
->set(
'tax_query',
[
// See this for specifics: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
[
// Change as needed
'taxonomy' => 'event-type',
'field' => 'term_id',
'terms' => [$category_id],
],
]
);
}
);
I'm trying to change wordpress default search behavior in order to consider taxonomies names too. If I inform on the search input the value 'Cybercops', I want the wordpress search to look for posts that have the cybercops word.
But the blog have a custom taxonomy named Heros, and one of the Heros categories could have the name 'Cybercops' too. So, If a post or custom post belongs to these 'Cybercops' Heros category, this posts has to appear on the result of the search.
So far, I have this, because I want to bring future post too:
function my_theme_my_custom_search( $query ) {
if( $query->is_main_query() && $query->is_search() && !is_admin()) {
$query->set( 'post_status', array('publish, future') );
}
}
add_action( 'pre_get_posts', 'my_theme_my_custom_search' );
I tried to use one more $query->set using tax_query but it doesn't work. Can someone help me?
You can use any of the arguments from the WP_Query.
Source # https://developer.wordpress.org/reference/classes/wp_query/
Parameter
Description
tax_query
(array) Use taxonomy parameters (available since version 3.1)
<?php
$tax = array(
array(
'taxonomy' => '_your_custom_taxonomy_',
'field' => 'slug',
'terms' => '_your_custom_term_slug_',
)
);
$query->set( 'tax_query', $tax );
?>
I bought a WordPress theme for affiliate marketing business. I have 1000+ stores on my website and hence hundred of coupons on homepage.I simply write a short-code to fetch all coupons and they started to appear on homepage .All coupons are based on different categories.
How can i exclude a number of coupons from homepage and that is possible only if we can exclude some categories from homepage based on their IDs.
I Google it many times but each time i found "pre_get_posts" function to do so. But in my case, it's not working at all. The code is below
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
Through this code i am trying to exclude desired categories. I also tried to modified above code in different ways but none of my solution/idea worked.
What i think is the reason behind why above code is no working is the function only works for default "Category" option under "Posts"on Dashboard. And my categories are coming from another option on Dashboard that's "Coupons" -> "Offer Categories".
See screenshot: Offer Categories
May be or may be not, i need to modify the above function in a way that it can select my expected categories. But as i have lack of knowledge in PHP so i don't know what and how to do?
I'm badly stuck here and hoping to get some help from experts.
Thanks in advance
Try this code.
if(is_home() || is_front_page()){
$args_active_coupons['tax_query'][] = array(
'taxonomy' => 'offer_categories',
'field' => 'id',
'terms' => array(214),
'operator' => 'NOT IN',
);
}
If you want to hard code all the excluded category, you have to put your category ID with (-) prefix. e.g
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-<CAT_ID>, -<CAT_ID>, -<CAT_ID>' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
Here: CAT_ID should be excluded category id.
In another way, you could use a plugin:
https://wordpress.org/plugins/ultimate-category-excluder/
I have this loop that gets chosen categories for a custom post type of portfolio:
$wp_query = new WP_Query(array('category__in' => $portfolio_cat, 'post_type' => 'portfolio', 'showposts'=>$number_of_posts) );
It uses standard wordpress categories, but only shows portfolio type posts in those categories. The problem is, I want to list the category the portfolio post is in but when the category is clicked it leads to a 404 not found.
I am using this to display the category (successfully) under the portfolio post type:
<?php the_category('') ?>
Is there any way to have a custom post type use standard categories and not a custom taxonomy and be able to click the category to go to a page that lists all posts in the category?
You have to use
'taxonomies' => array('category')
in your register_post_type() function to make it connected to the category. Also, you can use this
register_taxonomy_for_object_type('category','portfolio');
after you call the register_post_type() function.
If you want your custom post type posts to show up on standard archives or include them on your home page mixed up with other post types, use the pre_get_posts action hook.
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
{
$query->set( 'post_type', array( 'post', 'page', 'portfolio' ) );
}
return $query;
}
I have a made a custom post type called "Member Resources" the posts under this CPT have a few taxonomies such as categories and tags.
Tags = "Diversity" Categories = "Guidance"
When I go to the following urls:
www.domain.com/tags/diversity
www.domain.com/tags/guidance
No posts appear.
Though I have set public => true on the CPT function.
Posts are displaying if you go to the Member Resources archive page though, so they are displaying, but not when you filter them by taxonomies.
Update -
Adding the following code to my functions.php file allows the member-resources CPT to show in Category and Tags pages respectively, but now in the wordpress backend under the "Pages" tab and all other content tabs such as posts etc it seems to have overrided my pages and posts and is showing just the member-resources posts.
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
function add_my_custom_post_type( $query ) {
if ($query->is_main_query())
$query->set( 'post_type', array( 'member-resources' ) );
return $query;
}
your code looks correct. but you are including the CPT member-resources in too many of wordpress's queries. the is_main_query means "the loop" i think.
so you need to restrict this to just running when on a tag archive page.
the following code is from the wordpress site
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && is_main_query() && ! $query->get( 'cat' ) )
$query->set( 'cat', '-5' );
}
You need to do a similar thing but determine if you are in a "tags" page.