Why my custom posts not showing under categories - wordpress

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' );

Related

How can I replace a custom-post-type with a custom post category in permalink without getting a 404 in wordpress?

I'm a wordpress newbie. I will try to describe my problem in the clearest way possible.
I'm trying to do two things here:
Remove CPT from a permalink.
Add a custom taxonomy type where it used to be the CPT in the permalink.
Permalinks on site used to be like this:
http://example.com/custom-post-type/post-name
I managed to remove the CPT from the permalink based on this:
how to remove custom post type from wordpress url?
Then I modify my code to add the custom taxonomy type to the permalink to be like this:
http://example.com/post-category/post-name
This is my code:
function remove_cpt_slug( $post_link, $post ) {
if ( 'custom-post-type-name' === $post->post_type && 'publish' === $post->post_status ) {
$post_tags = get_the_terms($post->ID, 'custom-post-type-name-category');
$post_link = str_replace( '/' . $post->post_type . '/', '/' . $post_tags[0]->slug . '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 );
function add_cpt_post_names_to_main_query( $query ) {
// Return if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Return if this query doesn't match our very specific rewrite rule.
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
// Return if we're not querying based on the post name.
if ( empty( $query->query['name'] ) ) {
return;
}
// Add CPT to the list of post types WP will include when it queries based on the post name.
$query->set( 'post_type', array( 'post', 'page', 'custom-post-type-name' ) );
}
add_action( 'pre_get_posts', 'add_cpt_post_names_to_main_query' );
This worked but now every post from my CPT gives a 404. How can I solve this?

Can I exclude a specific category of a custom post type from both search and archives?

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' );

How to display a custom post type feed in the sidebar of a category page?

I have a custom post type setup and in the sidebar of every page on the site it is set to display a random post from that custom post type (named "reviews").
This works great everywhere except for category pages for the normal / standard / default post type of "post" where even though the query is setup to only use the custom post type "reviews" it only pulls from the default blog posts.
Is there something I am leaving out to make sure this works even on category pages?
Here is the code I am using that works fine on non category pages, you can see it is restricted to just the "reviews" post type:
// the query
$review_query = new WP_Query( array (
'post_type' => 'reviews', // Display just this post type
'orderby' => 'rand',
'posts_per_page' => 1,
)
);
You can create shortcode that you can put in any widget area you want.
Something like:
function wpb_rand_posts() {
if ( $the_query->have_posts() ) {
$string .= '<ul>';
while ( $the_query->have_posts() ) {
//do stuff
}
$string .= '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
$string .= 'no posts found';
}
return $string;
}
add_shortcode('wpb-random-posts','wpb_rand_posts');
add_filter('widget_text', 'do_shortcode');
Ok so it turns out the code I had added to functions.php to make sure a different custom post type was displaying in the category feed was interfering.
So this was the original code snip I used to do that:
// Show notable cases in tag archives
function themeprefix_show_cpt_archives( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'nav_menu_item', 'post', 'cases'
));
return $query;
}
}
And I changed that to this:
// Show notable cases in tag archives
function themeprefix_show_cpt_archives( $query ) {
if( empty( $query->query_vars['suppress_filters'] ) && ( is_category() || is_tag() ) ) {
$query->set( 'post_type', array(
'nav_menu_item', 'post', 'cases'
));
return $query;
}
}
And added suppress_filters' => true to my query, and this resolved my issue. If anyone else runs into this see what else in your theme may be modifying the query at a higher level through a function like this or plugin as this is a solution specific to the code in my theme.

WordPress WooCommerce after theme setup not working

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.

Can I make Wordpress searchbar filter to give only articles from blog and not from woocommerce?

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

Resources