Solved

Populate categories for custom post type

Posted on 2015-02-12
3
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to count occurrences of each item in an array.
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…

739 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