I just want to customize the theme of WooCommerce in my theme. But the problem is when I add the folder of WooCommerce template to override the WooCommerce theme and after adding this function:
function add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'add_woocommerce_support' );
The shop page is not displaying the list of products. Instead the archive post is displaying. But when I comment the add_action of woocommerce theme setup, everything is working fine. See this picture:
I also have this function to display my custom post type all post in archive page:
function add_cpt_program_in_archive( $query ) {
if ( !is_admin() && $query->is_archive() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'program' ) );
}
}
add_action( 'pre_get_posts', 'add_cpt_program_in_archive' );
I also try to add !is_shop() in the condition of my add_cpt_program_in_archive function. So its look like this:
function add_cpt_program_in_archive( $query ) {
if ( !is_admin() && $query->is_archive() && $query->is_main_query() && !is_shop() ) {
$query->set( 'post_type', array('post', 'program' ) );
}
}
add_action( 'pre_get_posts', 'add_cpt_program_in_archive' );
The shop page is now displaying the list of products. But the problem is when I visit the category page of product. Example the uncategorized category. It's not displaying the list of product that was in the uncategorized category. See this picture:
I found the solution. I add this code !$query->query_vars['product_cat']) in the condition of my add_cpt_program_in_archive function. So its look like this
function add_cpt_program_in_archive( $query ) {
if ( !is_admin() && $query->is_archive() && $query->is_main_query() && !is_shop() && !$query->query_vars['product_cat']) {
$query->set( 'post_type', array('post', 'program' ) );
}
}
add_action( 'pre_get_posts', 'add_cpt_program_in_archive' );
But I dont know if this is correct format or maybe you guys have a better suggestion answer rather than to my solution.
Related
I have a custom post type "courses" and the taxonomy for categories within this post type is "course-category."
I have a course category called "Academy" and I would like to exclude that category from any search, and from any archive pages for that custom post type.
Right now I have this code snippet but it doesn't seem to be working. It's for the search exclusion only but I need to exclude the category in both search pages and archive pages.
function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set('post_type', array('courses');
$query->set( 'course-category','-68' );
return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );
Have you tried using is_post_type_archive()?
function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() || $query->is_main_query() && !is_admin() && $query->is_post_type_archive('courses') ) {
$query->set('post_type', array('courses');
$query->set( 'course-category','-68' );
return $query;
}
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );
I have some pages that I don't want it to be included when anybody searches within my website. I tried to add the code below to my function.php but it doesn't work
add_filter( 'pre_get_posts', 'exclude_pages_search_when_logged_in' );
function exclude_pages_search_when_logged_in($query) {
if ( $query->is_search && is_user_logged_in() )
$query->set( 'post__not_in', array( 6410, 1684, 6385, 278, 6390, 865 ) );
return $query;
}
How do I exclude some pages from the search results?
add_action is what you use to create a trigger “hook” – when something happens, do-something-else.
add_filter is used to “hook” data change/replace. use the code below instead
add_action( 'pre_get_posts', 'my_search_exclude_filter' );
function my_search_exclude_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 9564, 1213 ) );
}
}
I also described the other solutions is my blog post https://naderzad.info/web-development/wordpress-search-result/
When using the searchbar in the "news" page of my Wordpress, it displays posts from the blog but also posts that are products from Woocommerce, or other pages / content. I would like it to display only blog posts.
I haven't found any solution yet. Also, Wordpress won't let me update the php files of the theme, so I think I need on-site settings / plugins.
Here is the news page : https://champagne-oudart.com/actualites/
The client wanted the news categories to be years.
I recommend to use a plugin like Relevanssi https://wordpress.org/plugins/relevanssi/
It enables you to limit/control the search in many ways, also the one you asked for, and it's very simple to handle.
function modify_search_query( $query ) {
// Make sure this isn't the admin or is the main query
if( is_admin() || ! $query->is_main_query() ) {
return;
}
// Make sure this isn't the WooCommerce product search form
if( isset($_GET['post_type']) && ($_GET['post_type'] == 'product') ) {
return;
}
if( $query->is_search() ) {
$in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) );
// The post types you're removing (example: 'product' and 'page')
$post_types_to_remove = array( 'product', 'page' );
foreach( $post_types_to_remove as $post_type_to_remove ) {
if( is_array( $in_search_post_types ) && in_array( $post_type_to_remove, $in_search_post_types ) ) {
unset( $in_search_post_types[ $post_type_to_remove ] );
$query->set( 'post_type', $in_search_post_types );
}
}
}
}
add_action( 'pre_get_posts', 'modify_search_query' );
add these lines to your functions.php
how can I make my categories display posts in randomised orded instead of new to old in wordpress?
simple function through pre_get_posts hook will achieve the desired result
function change_order_by( $query ) {
if ( is_archive() && $query->is_main_query() ) {
$query->set( 'orderby', 'rand' );
}
}
add_action( 'pre_get_posts', 'change_order_by' );
copy the function above to your functions.php and you are good to go.
I have a custom post type called reviews. I have added a category taxonomy and had added my custom post types under the new category taxonomy. When I click on that category it doesn't show any of my custom posts. It will only show regular posts.
Any suggestions on how I can fix that?
use the below function and put this on your themes functions.php
function add_reviews_post_type( $query ) {
if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'reviews'
));
}
return $query;
}
add_filter( 'pre_get_posts', 'add_reviews_post_type' );