Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populate categories for custom post type

Posted on 2015-02-12
3
Medium Priority
?
185 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
There is a massive demand for content on the web right now, and it doesn't look like it's going to stop any time soon. But, if you are running a business blog, it's not just enough to offer your audience lots of content. It needs to be high-quality…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

596 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