How to match posts and categories in WordPress

match products and categories in WooCommerce

An optimized solution to match selected products, categories and other terms

Get options data or post meta in WordPress

First of all, get your saved data from the database. Data can be saved in options or post meta. Use get_option() method or get_post_meta_method() to retrieve data.

$selected_products    = get_option( {option name} );
$selected_categories = get_option( {option name} );
//Rule-based post meta
$selected_products    = get_post_meta( {post id}, {meta_key}, true );
$selected_categories = get_post_meta( {post id}, {meta_key}, true );

Store array in WordPress PHP

There are two ways to store data in options and post meta or WordPress database. 

  • Use wp_json_encode() or json_encode() method to convert array to string in a formatted way.
  • Use serialize() method to serialize the data and store it.
Read in detail about wp_json_encode method.

Note: wp_json_encode() method is encouraged and more reliable method to safely store the data. You may lose values in some cases if you are using serialize method.

Convert formatted string to array PHP

In this phase, you can use json_decode() for JSON encoded data and unserialize() method for serialized data.

// Example Code for json_decode
$selected_products	 = json_decode( get_option( '_products' ) );
$selected_categories   = json_decode( get_option( '_categories' ) );

$selected_products     = json_decode( get_post_meta( $post_id, '_products', true ) );
$selected_categories  = json_decode( get_post_meta( $post_id, '_categories', true ) );

// Example code for serialized data
$selected_products	  = unserialize( (string) get_option( '_products' ) );
$selected_categories  = unserialize( (string) get_option( '_categories' ) );

$selected_products 	= unserialize( (string) get_post_meta( $post_id, '_products', true ) );
$selected_categories 	= unserialize( (string) get_post_meta( $post_id, '_categories', true ) );

Match products in WooCommerce 

global $product;

$product_flag = false;

if( in_array( $product->get_id(), (array) ${variable name for selected products} ) ){
	$product_flag = true;
}

Match categories in WooCommerce

$category_flag = false;
if( !empty( $selected_categories ) && has_term( $selected_categories, 'product_cat', $product->get_id() ) ){
       $category_flag = true;
}

Check product or category is matched

At last, check whether any of the products or categories match the current product.

if( $product_flag || $category_flag ){
	
	// Do your task. 
}

Match AND or OR relations of products and categories

$selected_products 	= unserialize( (string) get_post_meta( $post_id, '_products', true ) );
$selected_categories 	= unserialize( (string) get_post_meta( $post_id, '_categories', true ) );

global $product;

$product_flag = false;

// Product Match
if( in_array( $product->get_id(), (array) $selected_products ) ){
	$product_flag = true;
	echo 'current product matched with selected products;
}

// Category match
$category_flag = false;

if( !empty( $selected_categories ) && has_term( $selected_categories, 'product_cat', $product->get_id() ) ){
	$category_flag = true;
	echo 'current product belongs to selected categories;
	break;
}

// Either belong to any category or are included in the list of products array.

if( $product_flag || $category_flag ){
	
	echo 'current product match with a selected product or any of selected categories;
}

// It must belong to any product category and be included in the list of products.
if( $product_flag && $category_flag ){
	
	echo 'current product match with selected product and selected categories;
}

Match terms of posts and products

has_term() method is used to match categories, tags, and other terms in WordPress and WooCommerce.

Match post categories.

Ex. has_term( $post_catogires_ids, 'category', $post_id )

Match product categories.

has_term( $product_catogires_ids, 'product_cat', $product_id );

Match post tags.

has_term( $post_tags_ids, 'tag', $post_id );

Match product tags.

has_term( $product_tags_ids, 'product_tag', $product_id );

Leave a Reply

%d