Customize Wordpress date archive widget for categories - php

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

Related

is there anyway to create link for specific products in shop page wordpress woocommerce?

I am trying to find a way to create a link to my shop page in woo commerce WordPress
for example, let's say I have 10 products
target products: 1, 3
and I want to send my friend a link that has two products (1,3) I want to show him.
so is there any way to create a link like that
www.mywebiste/shop/product-1,product-2
if that idea doesn't work, can you share with me some opinions that would help me?
Add this code to your theme functions.php file.
add_filter( 'pre_get_posts', function ( $query ) {
if ( is_shop() && $query->is_main_query() ) {
if ( ! empty( $_GET['share_products'] ) ) {
$products = sanitize_text_field( $_GET['share_products'] );
$inc_product = explode( ',', $products );
if ( ! empty( $inc_product ) ) {
$query->set( 'post__in', $inc_product );
}
}
}
return $query;
} );
Then you can share your shop page link with products ID. like this (comma seperated product IDs),
www.mywebiste/shop/?share_products=54,41

How to exclude a specific category from blog posts

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

Filter products Woocommerce by ID

I am trying to change the query of the shop (archive) of Woocommerce. I searched and found a lot of snippets but didn't find my solution yet. So I hope someone here can help me out.
What I want is to hide a selection of products which are defined by a separate function. The function returns an array of product/post id's. I can't find out why this is not working...
function shop_show_products_by_id( $meta_query, $query ) {
// Only on shop archive pages
if( is_admin() || is_search() || ! is_shop() ) return $meta_query;
$meta_query[] = array(
'post__not_in' => gives_product_ids_not_to_show()
);
return $meta_query;
}
add_filter( 'woocommerce_product_query_meta_query', 'shop_show_products_by_id', 10, 2 );
post__not_in isn't a meta_query...
You can exclude products like this, using the woocommerce_product_query which is essentially pre_get_posts. If gives_product_ids_not_to_show() returns an array of integers, you can replace the array() after the post__not_in
function shop_hide_products_by_id( $q ) {
// Only on shop archive pages
if( is_shop() ){
$q->set('post__not_in' , array(89,2182)); // use integers
}
}
add_filter( 'woocommerce_product_query', 'shop_hide_products_by_id' );

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;
}

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;
}
}

Categories