0

So I have both a custom post type which is called 'Recipes' in my register method() as shown below:

public static function register()
{
    add_action('init', function() {
        $labels = array(
            'name' => __('Recipes'),
            'singular_name' => __('Recipe'),
            'nickname' => __('Recipe'),
            'add_new' => _x('Add Recipe', 'Recipe'),
            'edit_item' => __('Edit Recipe'),
        );
        $args = array(
            'labels' => $labels,
            'slug' => 'recipe',
            'plural' => 'Recipes',
            'public' => true, // Adds the 'View' option.
            'show_ui' => true,
            'show_in_admin_bar' => true,
            'show_in_rest' => true, // Show the new Gutenberg editor
            'supports' => array('title', 'editor', 'thumbnail'),
            'taxonomies' => array('recipe_categories'),
        );
        register_post_type('recipe', $args);

        $labels = array(
            'name'              => _x( 'Categories', 'taxonomy general name', 'textdomain' ),
            'singular_name'     => _x( 'Category', 'taxonomy singular name', 'textdomain' ),
            'search_items'      => __( 'Search Categories', 'textdomain' ),
            'all_items'         => __( 'All Categories', 'textdomain' ),
            'parent_item'       => __( 'Parent Category', 'textdomain' ),
            'parent_item_colon' => __( 'Parent Category:', 'textdomain' ),
            'edit_item'         => __( 'Edit Category', 'textdomain' ),
            'update_item'       => __( 'Update Category', 'textdomain' ),
            'add_new_item'      => __( 'Add New Category', 'textdomain' ),
            'new_item_name'     => __( 'New Category Name', 'textdomain' ),
            'menu_name'         => __( 'Categories', 'textdomain' ),
        );

        $args = array(
            'hierarchical'      => true,
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => array('slug' => 'recipe_categories'),
        );
        register_taxonomy('recipe_categories', array('recipe'), $args);
    });

This created both my custom post type and my custom taxonomy - I've assigned the custom category to one of my posts: enter image description here

It's showing in the database under wp_term_taxonomy that it's being used as the count has risen: enter image description here

Inside the wp_posts table, under my single post it doesn't have any indication of categories (Unsure if it's supposed to be like this) enter image description here

Then I have a get() method that retrieves the meta using WP_Query:

public static function get()
{
    $recipes = [];

    $terms = get_terms('recipe_categories');

    $args = [
        'post_type' => 'recipe',
        'post_status' => 'publish',
        'orderby' => 'title',
        'order' => 'ASC',
        'posts_per_page' => 10,
    ];

    $recipe_query = new WP_Query($args);

    var_dump($recipe_query);

    foreach ($recipe_query->posts as $post) {
        $recipes[] = Recipe::init($post->ID);
    }

    return $recipes;
}

I get the object with posts but nothing that is linking to an 'taxonomies' as shown below:

enter image description here

Does anyone know what I might be missing?

0

WP_Query only returns WP_Post objects for the results. To get the taxonomy terms for each post you need to use get_the_terms( $post, 'recipe_categories' ); for each post.

This is because WP_Post represents each row of wp_posts, which does not include taxonomy information. The links between posts and terms is in the wp_term_relationships table.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.