Show only taxonomy types terms associated with a custom post type in WordPress PHP

UPDATE

The custom SQL query below can be used to produce a list of unique types taxonomy terms that have been assigned to newsroom posts. In PHP, you can call the function get_newsroom_types_terms to get the list: $terms = get_newsroom_types_terms();.

Custom SQL query

function get_newsroom_types_terms() {
    global $wpdb;
    $table_prefix = $wpdb->prefix;
    $taxonomy = 'types';
    $post_type="newsroom";

    $sql = <<<EOSQL
        SELECT DISTINCT T.term_id, T.name, T.slug
        FROM {$table_prefix}terms T
        JOIN {$table_prefix}term_taxonomy TT ON ( TT.term_id = T.term_id )
        JOIN {$table_prefix}term_relationships TR ON ( TR.term_taxonomy_id = TT.term_taxonomy_id )
        JOIN {$table_prefix}posts P ON ( P.ID = TR.object_id )
        WHERE TT.taxonomy = %s
            AND P.post_type = %s
        ORDER BY T.name
    EOSQL;

    $query = $wpdb->prepare( $sql, $taxonomy, $post_type );

    $terms = $wpdb->get_results( $query, \OBJECT );

    return $terms;
}

HTML Form

Please see my comment.

The PHP/HTML code for the form has several problems.

  1. It partially hardcodes the URL for admin-ajax.php.
  2. admin-ajax.php is not requested from an AJAX call.
  3. There is no indication of the default state for checkboxes.
  4. There is no code indicating how checkbox states are updated.
  5. Material-UI class names are hardcoded even though Material-UI uses ReactJS to render its HTML.

Please read the WordPress Documentation on How Do I Use AJAX?

tech