Solved

Populate categories for custom post type

Posted on 2015-02-12
3
139 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

786 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