How to exclude a specific category from blog posts - php

How to exclude a specific category from blog posts in WordPress but if a blog post has two categories selected, one from excluded category and the other from included I would like to show the blog post.
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-7' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
I tried using this function in function.php, it hides the excluded category but when two categories are selected for one blog post, it still hides

/* you may try this code and use category__not_in function */
function exclude_posts_from_home_page( $query )
{
if( $query->is_home() )
{
$query->set( 'category__not_in', array( 7 ) ); // here array of all category ids
}
}
add_action( 'pre_get_posts', 'exclude_posts_from_home_page' );

Related

how to display custom post type in archive.php?

I created custom post type with general categories (for default posts and custom posts the same categories) through the Custom Post Type UI plugin.
How to make on the category page so that, in addition to defaulted posts, custom posts for the same category are also displayed? Thanks
Create a function like this:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( (is_category() or is_archive() ) && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'your_post_type' ) );
return $query;
}

Customize Wordpress date archive widget for categories

I'm trying to modify the Wordpress theme's date archive widget to use it for every category.
With the following code it's possible to show only posts of an specific category, but I have to paste the categories_id as a fixed value. I want to do it dynamically.
I've tried to retrieve the category_id by using get_queried_object(), but it doesn't seem to work.
What is wrong with the code?
function wpse75668_filter_pre_get_posts( $query ) {
$page_object = get_queried_object();
$idObj = get_category_by_slug("'".$page_object->cat_name."'");
$id = $idObj->term_id;
if ( $query->is_main_query() && ! is_admin() ) {
// Only modify date-based archives
if ( is_date() ) {
// Only display posts from category ID
$query->set( 'cat', "'".$id."'" );
}
}
}
add_action( 'pre_get_posts', 'wpse75668_filter_pre_get_posts' );

WooCommerce: Display product variations inside of loop

How can I display the product variations for each product within a loop such as the one on the Shop page? Is there any function that I can add to the content-product template that retrieves the list of variations and displays them?
You can use this code to add product variation to the shop/product category loops
// Load our function when hook is set
add_action( 'pre_get_posts', 'custom_modify_query_get_posts_by_date' );
// Modify the current query
function custom_modify_query_get_posts_by_date( $query ) {
// Check if on frontend and main query is modified and if its shop or product category loop
if( (! is_admin() && $query->is_main_query()) && ( is_shop() || is_product_category() ) ) {
$query->set( 'order', 'ASC' );
add_filter( 'posts_where', 'rc_filter_where' );
}
return $query;
}
// Add products variation post type to the loop
function rc_filter_where( $where = '' ) {
$type = 'product_variation';
$where .= " OR post_type = '$type'";
return $where;
}
Best way to do this is alter the loop. Something like that will help:
function filter_wc_query($query_args){
if(is_archive()){ //here you can use any conditional function to show variations on
$query_args[] = ['post_parent'=> '*'];
}
return $query_args;
}
add_filter('woocommerce_get_query_vars','filter_wc_query');
Or if you want to this on view level:
https://gist.github.com/lukecav/2470d9fe6337e13da4faae2f6d5fe15f
In this solution image you can grab by standard WP function:
get_post_thumbnail($product->ID,'shop_list');
But remeber that variation link will bring user to parent product and force him to select variation once more.

How to add post tags to WooCommerce products?

I noticed that post tags are different than WooCommerce product tags.
I need to add post tags to WooCommerce products so i can include some WooCommerce products in the post tag archives.
Is this possible?
I've tried these code snippets but it doesn't add them.
add_filter( 'pre_get_posts', 'add_custom_types' );
function add_custom_types( $query ) {
if ( is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array( 'post', 'products', 'product' ) );
return $query;
}
}
add_filter('request', 'post_type_tags_fix');
function post_type_tags_fix($request) {
if ( isset($request['tag']) && !isset($request['post_type']) )
$request['post_type'] = array( 'products', 'product' );
return $request;
}
I needed to do this as well and have come up with what I think is a good way of doing this. A few steps:
Firstly, you need to add the post_tag taxonomy to the woocommerce product post type. You can do this easily with the following filter:
function custom_wc_add_post_tags( $args ){
$args['taxonomies'] = array('post_tag');
return $args;
}
add_filter( 'woocommerce_register_post_type_product', 'custom_wc_add_post_tags' );
This will add a new 'Tags' item to your admin menu, and allow you to tag products with regular post tags rather that the woocommerce-specific 'product_tag' taxonomy. Secondly, you'll likely want to remove the 'product_tag' taxonomy if you don't plan on using it? Because the above will result in two admin menu items called 'tags' which will get confusing. The following will do that for you:
add_filter('woocommerce_taxonomy_objects_product_tag', '__return_empty_array');
add_filter('woocommerce_taxonomy_args_product_tag', '__return_empty_array');
And to remove the column from the 'Products' table on the backend:
function custom_wc_remove_product_tags_column($columns){
unset( $columns['product_tag'] );
return $columns;
}
add_filter( 'manage_edit-product_columns', 'custom_wc_remove_product_tags_column', 15 );
That will actually add the tags to your products. If you then need these to show up in your archive page, you may still need to modify that page's query to look for posts of type 'product' as well as the standard 'post'. It looks like your function above on the 'pre_get_posts' hook will do that part for you. Hope this helps!
Use below code to get archive page in product tag page. now product tag page use theme archive.php file.
Add Below code in theme functions.php
add_filter( 'template_include', 'woocommerce_product_tag_page_template', 99 );
if ( ! function_exists( 'hcode_post_format_parameter' ) ) {
function woocommerce_product_tag_page_template( $template ) {
if ( is_tax( 'product_tag' ) ) {
get_template_part('archive');
}
return $template;
}
}

Difference between add_filter versus add_action

I was looking over my functions.php and wondering why CODE A uses add_action while CODE B uses add_filter ?
The main goal of CODE A is to both include and exclude specific categories.
The main goal of CODE B is to exclude specific categories.
Is it correct to use add_action for CODE A
and add_filter for CODE B?
CODE A: Display specific category (called "featured") for homepage, instead of "the most recent posts"
function featured_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category_name', 'featured' );
$query->set( 'category__not_in', array(60, 61) );
$query->set( 'posts_per_page', 5 );
}
}
add_action( 'pre_get_posts', 'featured_category' );
CODE B: Exclude "sponsored posts categories" for search results
function search_filter($query) {
if ( $query->is_search && $query->is_main_query() ) {
$query->set('post_type', 'post');
$query->set( 'category__not_in', array(60, 61) );
$query->set( 'posts_per_page', 20 );
}
return $query;
}
add_filter('pre_get_posts', 'search_filter');
pre_get_posts is an action and not a filter. $query is passed by reference which is why CODE A works without returning anything.
CODE B returns $query but again that code works because query has been passed by reference. The return value of the hook isn't assigned to anything.
do_action_ref_array( 'pre_get_posts', array( &$this ) );
add_action and add_filter are used in different contexts but the code is the same (add_action is an alias of add_filter). While both sets of code posted will work the correct usage is add_action.

Categories