categories – Can’t get selected value from wp_dropdown_categories or get_categories

Question

I’ve searched and found this and a few more. But I still couldn’t get the selected name (not value as value is just a number). get_categories keeps returns an empty array.

Below is the code from the plugin that I’m modding.

$categories = apply_filters( 'acadp_admin_listing_form_categories_dropdown', '', $post->ID );

        if ( empty( $categories ) ) {
            $selected_category = count( $category ) ? $category[0] : -1;
            
            $args = array(
                'show_option_none' => '-- ' . esc_html__( 'Select category', 'advanced-classifieds-and-directory-pro' ) . ' --',
                'taxonomy'         => 'acadp_categories',
                'name'             => 'acadp_category',
                'orderby'          => 'name',
                'selected'         => (int) $selected_category,
                'hierarchical'     => true,
                'depth'            => 10,
                'show_count'       => false,
                'hide_empty'       => false,
                'echo'             => false,
            );
            
            if ( $disable_parent_categories ) {
                $args['walker'] = new ACADP_Walker_CategoryDropdown;
            }
            
            $categories = wp_dropdown_categories( $args );
        }

        echo $categories; 

in progress 0
ratib90486 1 year 2022-01-03T21:23:06-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2022-01-07T01:48:24-05:00

    I figured it out. To get the selected category, I don’t need all the codes above. I just need the below.

    $category = wp_get_object_terms( $post->ID, 'acadp_categories' );
    $selected_category = $category[0]->name;
    

Leave an answer

Browse
Browse