Show categorys with a certain meta value

Viewed 202

I added a custom meta to my categories, and I want to just show the categories that have a certain meta value in dokan new product category dropdown, but I don't know how to filter the category by meta value in the args of the function that gets the categories.

That´s the function that gets the categories.

$term = array();
$term = wp_get_post_terms( $post_id, 'product_cat', array( 'fields' => 'ids') );
include_once DOKAN_LIB_DIR.'/class.taxonomy-walker.php';
$drop_down_category = wp_dropdown_categories( apply_filters( 'dokan_product_cat_dropdown_args', array(
    'show_option_none' => __( '', 'dokan-lite' ),
    'hierarchical'     => 1,
    'hide_empty'       => 0,
    'name'             => 'product_cat[]',
    'id'               => 'product_cat',
    'taxonomy'         => 'product_cat',
    'title_li'         => '',
    'class'            => 'product_cat dokan-form-control dokan-select2',
    'exclude'          => '',
    'selected'         => $term,
    'echo'             => 0,
    'walker'           => new TaxonomyDropdown( $post_id )
) ) );
2 Answers

I discovered that you can filter the category by meta key and value by adding these keys to the arguments array: meta_key, meta_value, and meta_compare.

<?php
$term = array();
$term = wp_get_post_terms( $post_id, 'product_cat', array( 'fields' => 'ids') );
include_once DOKAN_LIB_DIR.'/class.taxonomy-walker.php';
$drop_down_category = wp_dropdown_categories( apply_filters( 'dokan_product_cat_dropdown_args', array(
    'show_option_none' => __( '', 'dokan-lite' ),
    'hierarchical'     => 1,
    'hide_empty'       => 0,
    'name'             => 'product_cat[]',
    'id'               => 'product_cat',
    'taxonomy'         => 'product_cat',
    'title_li'         => '',
    'class'            => 'product_cat dokan-form-control dokan-select2',
    'exclude'          => '',
    'selected'         => $term,
    'echo'             => 0,
    'meta_key'     => 'cat_plataform',
    'meta_value'   => 'normal',
    'meta_compare' => '==',
    'walker'           => new TaxonomyDropdown( $post_id )
) ) );
echo str_replace( '<select', '<select data-placeholder="' . esc_html__( 'Select product category', 'dokan-lite' ) . '" multiple="multiple" ', $drop_down_category );
Related