I am building a custom plugin with a function so that it works on OxygenBuilder

54 views Asked by At

I am building a function that calls all the custom post types inside a drop-down list, and when choosing from that list, all the categories associated with that custom post type are shown in the other drop-down list.

//here Is my code for Oxygen Bulder

 $custom_post_types = get_post_types(array(
            'public'   => true,
        ) );
    $custom_post_types_array = array();

    foreach ($custom_post_types as $post_type) {
        $custom_post_types_array[] = $post_type;
    }


$this->addOptionControl(
  array(
    "type" => "dropdown",  
    'name' => __('Post Types'),
    'slug' => 'post_types',
    "default" => array_keys($custom_post_types_array),
  )
)->setValue($custom_post_types_array)->rebuildElementOnChange();
    
        $categories = get_categories(array('taxonomy' => 'category'));
        $options = [];
    
        foreach ($categories as $category) {
            $options[$category->slug] = $category->name;
        }
    
        $this->addOptionControl(
            array(
                "type" => "dropdown",
                'name' => __('Category'),
                'slug' => 'cat_posts',
                "default" => 'Uncategorized',
            )
        )->setValue($options)->rebuildElementOnChange(function ($element) {
            return $element->getOption('cat_posts') !== 'uncategorized';
        });

I don't know the correct way to work with Oxygen Builder until it works properly

All I want is that when I click on the drop-down list for custom post type, the other drop-down list for the categories associated with that custom post type will be updated.

0

There are 0 answers