Solved

Populate categories for custom post type

Posted on 2015-02-12
3
135 Views
Last Modified: 2015-03-17
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

0
Comment
Question by:dirkil2
  • 2
3 Comments
 

Author Comment

by:dirkil2
ID: 40651064
Is there really nobody who can help me with that?
0
 
LVL 28

Accepted Solution

by:
chilternPC earned 500 total points
ID: 40669460
I would use a plugin to generate custom Post types
 such as
https://wordpress.org/plugins/custom-post-type-ui/
or
https://wordpress.org/plugins/types/
0
 

Author Comment

by:dirkil2
ID: 40670113
That is a good idea. Thank you very much.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now