skip to Main Content

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


  1. Chosen as BEST ANSWER

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

  2. You may refer to the WP_TERM_QUERY class.

    https://developer.wordpress.org/reference/classes/wp_term_query/__construct/

    The example code as below:

    $term = wp_get_post_terms( $post_id, 'product_cat', array( 'fields' => 'ids', 'meta_key' => 'your-meta-key', 'meta_value' => 'your-meta-value', 'meta_compare' => '=') );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search