Link to home
Start Free TrialLog in
Avatar of dirkil2
dirkil2

asked on

Populate categories for custom post type

Hi there!

In my Wordpress theme I want to have a custom post type and for that custom post type some categories. My problem is how to get the actual values for the categories into the database. As far as I understood it I have to populate the category values when the theme is activated. I tried that but I might be doing something completely wrong (see code below).

When I try to use my code the custom post type and the category are visible but there are no values in my category. I would expect to see "Sales" and "Rental" in there.

I debugged it a little and found that the call to wp_insert_term returns an error:
object(WP_Error)#124 (2) { ["errors":"WP_Error":private]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid taxonomy" } } ["error_data":"WP_Error":private]=> array(0) { } }


As far as I see it this is clear because callbacks in 'after_setup_theme' are executed before callbacks in 'init'. Since my custom post type is registered in 'init' and the call to wp_insert_term is before that, it must fail. But I also read that custom post types should be registered in 'init' and not in 'after_setup_theme'. So I am stuck here!

I am completely confused how to set this up correctly. Can somebody please explain to be how setting up a custom post type with categories is supposed to be done?

Many thanks in advance,
Dirk.
 
functions.php:

require_once ('libs/theme-setup.php');
require_once ('libs/navwalker.php');

add_action('after_setup_theme', 'wp_estate_init');
function wp_estate_init() {
  wp_theme_setup();    
}

add_action('init', 'ps_create_property_type');
function ps_create_property_type() {

    register_post_type('ps_immobilie', array(
        'labels' => array(
            'name'                  => __('Immobilien','ps_immobilie'),
            'singular_name'         => __('Immobilie','ps_immobilie'),
            'add_new'               => __('Neue Immobilie','ps_immobilie'),
            'add_new_item'          => __('Neu','ps_immobilie'),
            'edit'                  => __('Bearbeiten','ps_immobilie'),
            'edit_item'             => __('Immobilie bearbeiten','ps_immobilie'),
            'new_item'              => __('Neue Immobilie','ps_immobilie'),
            'view'                  => __('Anzeigen','ps_immobilie'),
            'view_item'             => __('Immobilie anzeigen','ps_immobilie'),
            'search_items'          => __('Immobilie suchen','ps_immobilie'),
            'not_found'             => __('Keine Immobilien gefunden','ps_immobilie'),
            'not_found_in_trash'    => __('Keine Immobilie im Papierkorb gefunden','ps_immobilie'),
            'parent'                => __('Übergeordnete Immobilie','ps_immobilie')
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'immo'),
        'supports' => array('title', 'editor', 'thumbnail'),
        'can_export' => true,
        'register_meta_box_cb' => 'ps_immobilie_add_immobilie_metaboxes'
         )
    );
    
    register_taxonomy('marketingtype_category', 'ps_immobilie', array(
        'labels' => array(
            'name'              => __('Immobilienart','ps_immobilie'),
            'add_new_item'      => __('Neue Immobilienart','ps_immobilie'),
            'new_item_name'     => __('Neue Immobilienart','ps_immobilie')
        ),
        'hierarchical'  => true,
        'query_var'     => true,
        'rewrite'       => array( 'slug' => 'immoart' )
        )
    );
	
register_nav_menu('primary', 'Hauptmenü');

add_filter( 'cmb2_meta_boxes', 'cmb2_immobilie_metaboxes' );

Open in new window


theme-setup.php:
function wp_theme_setup() 
{
	if (isset($_GET['activated']) && is_admin())
	{        
        $actions = array(   'Sales',
                            'Rental'
                        );
        foreach ($actions as $key) {
            $my_cat = array(
                'description' => $key,
                'slug' => $key
            );

            if(!term_exists($key, 'marketingtype_category') ){
                wp_insert_term($key, 'marketingtype_category', $my_cat);
            }
        }
     }
}

Open in new window

Avatar of dirkil2
dirkil2

ASKER

Is there really nobody who can help me with that?
ASKER CERTIFIED SOLUTION
Avatar of Peter Hart
Peter Hart
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dirkil2

ASKER

That is a good idea. Thank you very much.