0

I'm trying to create multiple taxonomies. As follows.....

$taxonomies = $wpdb->get_results( "SELECT * FROM $table_name" );
foreach ($taxonomies as $taxonomy) {
  //stuff that defines the taxonomy
register_taxonomy( $taxonomy->singular_name, 'book', $tagArgs );
}

It appears that WP dislikes regestering the taxonomy with $taxonomy->singular_name. Xdebug givies me the following error.

Notice: Trying to get property of non-object in /home/wpplugin/public_html/wp-includes/link-template.php on line 898

I've tried several diffent things. Converting the object to an array and feeding that to register_taxonomy(), I've tride casting $taxonomy->singular_name as a string and a few other random guessees that yeilded the same result. The only thing that works is putting a quoted string in place of $taxonomy->singular_name which won't fit my needs.

Does any one have any ideas on this?? Thanks Greg

Full code here.....

add_action( 'init', 'create_book_taxonomies', 0 );function create_book_taxonomies() {
global $wpdb;
$table_name = $wpdb->prefix . 'wlc_custom_taxonomy';
$taxonomies = $wpdb->get_results( "SELECT * FROM $table_name" );
foreach ($taxonomies as $taxonomy) {
    $enterMeOnce = $taxonomy->singular_name;
    $book = $taxonomy->singular_name;

    if($taxonomy->singular_name != 'singular-name'){ //exclude the default table row
        $resLabels = array(
                'name'               => _x( $enterMeOnce , 'Post Type General Name' ),
                'singular_name'      => _x( $enterMeOnce , 'Post Type Singular Name' ),
                'menu_name'          => __( $enterMeOnce  ),
                'name_admin_bar'     => __( $enterMeOnce  ),
                'all_items'          => __( $enterMeOnce  ),
                'add_new_item'       => __( 'Add New ' . $enterMeOnce  ),
                'edit_item'          => __( 'Edit ' . $enterMeOnce  ),
                'new_item'           => __( 'New ' . $enterMeOnce  ),
                'view_item'          => __( 'View ' . $enterMeOnce  ),
                'search_item'        => __( 'Search ' . $enterMeOnce  ),
                'not_found'          => __( 'No '.$enterMeOnce .' found' ),
                'not_found_in_trash' => __( 'No '.$enterMeOnce .' found in trash' ),
                'description'         => __( $enterMeOnce .' are special posts that are showcased on the '.$enterMeOnce .' page.' ),
            );
        $resArgs = array(
                'labels' => $resLabels,

                'public'              => true,
                'exclude_from_search' => false,
                'publcly_queryable'   => true,
                'show_ui'             => true,
                'show_in_menu'        => true,
                'show_in_nav_menus'   => true,
                'show_in_admin_bar'   => true,
                'menu_position'       => 5,
                'menu_icon'           => 'dashicons-lightbulb',
                'capability_type'     => 'post',
                'meta_box_cb'         => true,
                'show_admin_column'   => true,
                'hierarchical'        => true,
                'has_archive'         => true,
                //'rewrite'           => true,
                /*'capabilities'        => array(
                    'manage_terms' => 'manage_resource',
                    'edit_terms'   => 'manage_categories',
                    'delete_terms' => 'manage_categories',
                    'assign_terms' => 'edit_posts'
                ),*/
                'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'post_format' )
            );

    register_post_type( $book , $resArgs );

        $labels = array(
                'name'                       => _x( $taxonomy->name . ' Categories', 'Taxonomy General Name', 'text_domain' ),
                'singular_name'              => _x( $taxonomy->name . ' Category', 'Taxonomy Singular Name', 'text_domain' ),
                'menu_name'                  => __( $taxonomy->name . ' Categories', 'text_domain' ),
                'all_items'                  => __( 'All Categories', 'text_domain' ),
                'parent_item'                => __( 'Parent Category', 'text_domain' ),
                'parent_item_colon'          => __( 'Parent Category:', 'text_domain' ),
                'new_item_name'              => __( 'New Resource Category', 'text_domain' ),
                'add_new_item'               => __( 'Add New Resource Category', 'text_domain' ),
                'edit_item'                  => __( 'Edit Category', 'text_domain' ),
                'update_item'                => __( 'Update Category', 'text_domain' ),
                'separate_items_with_commas' => __( 'Separate resource cat names with commas', 'text_domain' ),
                'search_items'               => __( 'Search Resource Categories', 'text_domain' ),
                'add_or_remove_items'        => __( 'Add or remove resource categories', 'text_domain' ),
                'choose_from_most_used'      => __( 'Choose from the most used Categories', 'text_domain' ),
                'not_found'                  => __( 'Resource Category Not Found', 'text_domain' ),
                'slug'                       => __( $taxonomy->name . 'category' )
            );

            $args = array(
                'labels'                     => $labels,
                'hierarchical'               => true,
                'public'                     => true,
                'has_archive'                => true,
                'show_ui'                    => true,
                'show_admin_column'          => true,
                'show_in_nav_menus'          => true,
                'show_tagcloud'              => false,
                'rewrite'                    => true,
            );


            register_taxonomy( $taxonomy->singular_name, array( $book ), $args );

            $tagLabels = array(
                'name'                       => _x( 'Writers', 'taxonomy general name' ),
                'singular_name'              => _x( 'Writer', 'taxonomy singular name' ),
                'search_items'               => __( 'Search Writers' ),
                'popular_items'              => __( 'Popular Writers' ),
                'all_items'                  => __( 'All Writers' ),
                'parent_item'                => null,
                'parent_item_colon'          => null,
                'edit_item'                  => __( 'Edit Writer' ),
                'update_item'                => __( 'Update Writer' ),
                'add_new_item'               => __( 'Add New Writer' ),
                'new_item_name'              => __( 'New Writer Name' ),
                'separate_items_with_commas' => __( 'Separate writers with commas' ),
                'add_or_remove_items'        => __( 'Add or remove writers' ),
                'choose_from_most_used'      => __( 'Choose from the most used writers' ),
                'not_found'                  => __( 'No writers found.' ),
                'menu_name'                  => __( $taxonomy->name . ' Tags' ),
            );

            $tagArgs = array(
                'hierarchical'          => false,
                'labels'                => $tagLabels,
                'show_ui'               => true,
                'show_admin_column'     => true,
                'update_count_callback' => '_update_post_term_count',
                'query_var'             => true,
                'rewrite'               => array( 'slug' => 'writer' ),
            );

            register_taxonomy( $taxonomy->singular_name, $book, $tagArgs );


    }
}
}
8
  • Have you checked the data type of singular_name row in your table and the data type you have in $taxonomy->singular_name?
    – cybmeta
    Jan 28, 2015 at 19:30
  • The data type of the singular_name column is tinytext & var_dump($taxonomy->singular_name) reports it as a string. `public 'singular_name' => string 'Another' (length=7)'
    – user27251
    Jan 28, 2015 at 19:40
  • var_dump($taxonomy->singular_name) should be string(7) "Another" . Are you sure that you are doing var_dump($taxonomy->singular_name)? Can you post your full code?
    – cybmeta
    Jan 28, 2015 at 19:56
  • Full code posted above. I verified the var_dump and it was correct. i.e. string(7) "Another"
    – user27251
    Jan 28, 2015 at 20:46
  • Why are you registering two taxonomies with the same name for the same object type? I don't think you can do that.
    – cybmeta
    Jan 28, 2015 at 20:58

1 Answer 1

0

It turns out the issue here is the foreach statement. I changed the loop from

foreach ($taxonomies as $taxonomy) {

to

for ($counter=0; $counter < sizeof($taxonomy); $counter++) { 

as well as all relivant itirators so....

register_taxonomy( $taxonomy->singular_name, 'book', $tagArgs );

became

register_taxonomy( $taxonomy[$counter]->name, $book, $args );

and all went well.

I suspect this is a a problem with the memory of the host machine but I'm not sure if anyone has any insight as to why this worked I'd like to hear it. But for now this problem is solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.