get_the_terms example


function get_the_category( $post_id = false ) {
    $categories = get_the_terms( $post_id, 'category' );
    if ( ! $categories || is_wp_error( $categories ) ) {
        $categories = array();
    }

    $categories = array_values( $categories );

    foreach ( array_keys( $categories ) as $key ) {
        _make_cat_compat( $categories[ $key ] );
    }

    /** * Filters the array of categories to return for a post. * * @since 3.1.0 * @since 4.4.0 Added the `$post_id` parameter. * * @param WP_Term[] $categories An array of categories to return for the post. * @param int|false $post_id The post ID. */
if ( rest_is_field_included( 'meta', $fields ) ) {
            $data['meta'] = $this->meta->get_value( $post->ID, $request );
        }

        $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );

        foreach ( $taxonomies as $taxonomy ) {
            $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;

            if ( rest_is_field_included( $base$fields ) ) {
                $terms         = get_the_terms( $post$taxonomy->name );
                $data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
            }
        }

        $post_type_obj = get_post_type_object( $post->post_type );
        if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
            $permalink_template_requested = rest_is_field_included( 'permalink_template', $fields );
            $generated_slug_requested     = rest_is_field_included( 'generated_slug', $fields );

            if ( $permalink_template_requested || $generated_slug_requested ) {
                if ( ! function_exists( 'get_sample_permalink' ) ) {
                    

    $taxonomies = apply_filters( 'post_class_taxonomies', $taxonomies$post->ID, $classes$css_class );

    foreach ( (array) $taxonomies as $taxonomy ) {
        if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
            foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
                if ( empty( $term->slug ) ) {
                    continue;
                }

                $term_class = sanitize_html_class( $term->slug, $term->term_id );
                if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
                    $term_class = $term->term_id;
                }

                // 'post_tag' uses the 'tag' prefix for backward compatibility.                 if ( 'post_tag' === $taxonomy ) {
                    


    if ( ! $post->post_name ) {
        wp_update_post(
            array(
                'ID'        => $post_id,
                'post_name' => 'custom_slug_' . uniqid(),
            )
        );
    }

    $terms = get_the_terms( $post_id, 'wp_theme' );
    if ( ! is_array( $terms ) || ! count( $terms ) ) {
        wp_set_post_terms( $post_idget_stylesheet(), 'wp_theme' );
    }
}

/** * Generates a unique slug for templates. * * @access private * @since 5.8.0 * * @param string $override_slug The filtered value of the slug (starts as `null` from apply_filter). * @param string $slug The original/un-filtered slug (post_name). * @param int $post_id Post ID. * @param string $post_status No uniqueness checks are made if the post is still draft or pending. * @param string $post_type Post type. * @return string The original, desired slug. */

                'posts_per_page'      => 1,
                'no_found_rows'       => true,
                'lazy_load_term_meta' => false, // Do not lazy load term meta, as template parts only have one term.             )
        );
        $template_part_post  = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
        if ( $template_part_post ) {
            // A published post might already exist if this template part was customized elsewhere             // or if it's part of a customized template.             $content    = $template_part_post->post_content;
            $area_terms = get_the_terms( $template_part_post, 'wp_template_part_area' );
            if ( ! is_wp_error( $area_terms ) && false !== $area_terms ) {
                $area = $area_terms[0]->name;
            }
            /** * Fires when a block template part is loaded from a template post stored in the database. * * @since 5.9.0 * * @param string $template_part_id The requested template part namespaced to the theme. * @param array $attributes The block attributes. * @param WP_Post $template_part_post The template part post object. * @param string $content The template part content. */

    public function __get( $key ) {
        if ( 'page_template' === $key && $this->__isset( $key ) ) {
            return get_post_meta( $this->ID, '_wp_page_template', true );
        }

        if ( 'post_category' === $key ) {
            if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) {
                $terms = get_the_terms( $this, 'category' );
            }

            if ( empty( $terms ) ) {
                return array();
            }

            return wp_list_pluck( $terms, 'term_id' );
        }

        if ( 'tags_input' === $key ) {
            if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) {
                
if ( $clear_date ) {
        $postarr['post_date']     = current_time( 'mysql' );
        $postarr['post_date_gmt'] = '';
    }

    if ( 'attachment' === $postarr['post_type'] ) {
        return wp_insert_attachment( $postarr, false, 0, $wp_error );
    }

    // Discard 'tags_input' parameter if it's the same as existing post tags.     if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) {
        $tags      = get_the_terms( $postarr['ID'], 'post_tag' );
        $tag_names = array();

        if ( $tags && ! is_wp_error( $tags ) ) {
            $tag_names = wp_list_pluck( $tags, 'name' );
        }

        if ( $postarr['tags_input'] === $tag_names ) {
            unset( $postarr['tags_input'] );
        }
    }

    
function get_post_format( $post = null ) {
    $post = get_post( $post );

    if ( ! $post ) {
        return false;
    }

    if ( ! post_type_supports( $post->post_type, 'post-formats' ) ) {
        return false;
    }

    $_format = get_the_terms( $post->ID, 'post_format' );

    if ( empty( $_format ) ) {
        return false;
    }

    $format = reset( $_format );

    return str_replace( 'post-format-', '', $format->slug );
}

/** * Check if a post has any of the given formats, or any format. * * @since 3.1.0 * * @param string|string[] $format Optional. The format or formats to check. Default empty array. * @param WP_Post|int|null $post Optional. The post to check. Defaults to the current post in the loop. * @return bool True if the post has any of the given formats (or any format, if no format specified), * false otherwise. */

function _build_block_template_result_from_post( $post ) {
    $default_template_types = get_default_block_template_types();
    $terms                  = get_the_terms( $post, 'wp_theme' );

    if ( is_wp_error( $terms ) ) {
        return $terms;
    }

    if ( ! $terms ) {
        return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) );
    }

    $theme          = $terms[0]->name;
    $template_file  = _get_block_template_file( $post->post_type, $post->post_name );
    
$taxonomy = 'category';
        } elseif ( 'tags' === $column_name ) {
            $taxonomy = 'post_tag';
        } elseif ( str_starts_with( $column_name, 'taxonomy-' ) ) {
            $taxonomy = substr( $column_name, 9 );
        } else {
            $taxonomy = false;
        }

        if ( $taxonomy ) {
            $taxonomy_object = get_taxonomy( $taxonomy );
            $terms           = get_the_terms( $post->ID, $taxonomy );

            if ( is_array( $terms ) ) {
                $term_links = array();

                foreach ( $terms as $t ) {
                    $posts_in_term_qv = array();

                    if ( 'post' !== $post->post_type ) {
                        $posts_in_term_qv['post_type'] = $post->post_type;
                    }

                    
if ( rest_is_field_included( 'meta', $fields ) ) {
            $data['meta'] = $this->meta->get_value( $menu_item->ID, $request );
        }

        $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );

        foreach ( $taxonomies as $taxonomy ) {
            $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;

            if ( rest_is_field_included( $base$fields ) ) {
                $terms = get_the_terms( $item$taxonomy->name );
                if ( ! is_array( $terms ) ) {
                    continue;
                }
                $term_ids = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
                if ( 'nav_menu' === $taxonomy->name ) {
                    $data[ $base ] = $term_ids ? array_shift( $term_ids ) : 0;
                } else {
                    $data[ $base ] = $term_ids;
                }
            }
        }

        
if ( 'categories' === $column_name ) {
            $taxonomy = 'category';
        } elseif ( 'tags' === $column_name ) {
            $taxonomy = 'post_tag';
        } elseif ( str_starts_with( $column_name, 'taxonomy-' ) ) {
            $taxonomy = substr( $column_name, 9 );
        } else {
            $taxonomy = false;
        }

        if ( $taxonomy ) {
            $terms = get_the_terms( $post->ID, $taxonomy );

            if ( is_array( $terms ) ) {
                $output = array();

                foreach ( $terms as $t ) {
                    $posts_in_term_qv             = array();
                    $posts_in_term_qv['taxonomy'] = $taxonomy;
                    $posts_in_term_qv['term']     = $t->slug;

                    $output[] = sprintf(
                        '<a href="%s">%s</a>',
                        

function render_block_core_post_terms( $attributes$content$block ) {
    if ( ! isset( $block->context['postId'] ) || ! isset( $attributes['term'] ) ) {
        return '';
    }

    if ( ! is_taxonomy_viewable( $attributes['term'] ) ) {
        return '';
    }

    $post_terms = get_the_terms( $block->context['postId']$attributes['term'] );
    if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) {
        return '';
    }

    $classes = array( 'taxonomy-' . $attributes['term'] );
    if ( isset( $attributes['textAlign'] ) ) {
        $classes[] = 'has-text-align-' . $attributes['textAlign'];
    }
    if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
        $classes[] = 'has-link-color';
    }

    
Home | Imprint | This part of the site doesn't use cookies.