• Hi,

    I am building a plugin that requires me to register custom post types and terms.

    The post type has been registered correctly with:

    if (is_admin()) {
        require_once(plugin_dir_path(__FILE__) . 'classes/aggregator.php');
    
        add_action('init', array('Aggregator', 'init'));
        add_action('init', 'register_post_types');
    
    }
    
    function register_post_types()
    {
        register_post_type('social_post',
            array(
                'labels' => array(
                    'name' => __('Social Posts'),
                    'singular_name' => __('Social Post')
                ),
                'public' => true,
                'menu_icon' => 'dashicons-admin-users',
                'taxonomies' => array('area'),
                'has_archive' => true,
                'rewrite' => array('slug' => 'social-posts'),
            )
        );
    }
    
    register_taxonomy(
        'social_agg',
        'social_post',
        array(
            'label' => __('Social Aggregator Type'),
            'public' => true,
            'rewrite' => false,
            'hierarchical' => false,
        )
    );
    
    register_taxonomy('platform', array('game', 'post'), array(
        'label' => __('Social Aggregator Type'),
        'public' => true,
        'rewrite' => false,
        'hierarchical' => false,
    ));

    I also register the taxonomies in this block of code.

    When inserting the new terms I use the following:

    $types = ["Twitter", "Facebook", "YouTube"];
    
            foreach ($types as $type => $value) {
                wp_insert_term(
                    $value, // the term
                    'social_agg', // the taxonomy
                    array(
                        'description' => '',
                        'slug' => strtolower($value),
                        'parent' => 0
                    ));
            }

    Which seems to insert them correctly but always adds in the same amount but with IDs in them for values I guess they are aliasing them?

    Just wondering why they are doing this and if this is avoidable?

    Looking back at the code is it because the taxonomies aren’t registered in a function on init like the post type?

    Thanks,

    Harry

  • The topic ‘Custom Plugin dev insert custom term’ is closed to new replies.