• Resolved B1gJ4k3

    (@b1gj4k3)


    I’ve been developing a theme locally and having everything work just peachy. However, when I pushed the theme to a testing server today, I got the following error:

    Warning: Invalid argument supplied for foreach() in /home/theem3/theemerson.org/wp-includes/post.php on line 1038

    I looked into that file and found that it is the following function:

    foreach ( $args->taxonomies as $taxonomy ) {
    		register_taxonomy_for_object_type( $taxonomy, $post_type );
    	}

    This leads me to believe that I’ve done something weird when creating my custom taxonomy or custom post types. But, I followed the directions here to a T. Here’s my code that I’m working with.

    add_action( 'init', 'register_events' );
    function register_events() {
    	$labels = array(
    		'name' => 'Events',
    		'singular_name' => 'Event',
    		'add_new' => 'Add New Event',
    		'add_new_item' => 'Add New Event',
    		'edit' => 'Edit Event',
    		'edit_item' => 'Edit Event',
    		'view' => 'View Event',
    		'view_item' => 'View Event',
    		'search_items' => 'Search Events',
    		'not_found' => 'No Events found',
    		'not_found_in_trash' => 'No Events found in Trash',
    	);
    	$args = array (
    		'labels' => $labels,
    		'description' => 'Events custom post type',
    		'show_ul' => true,
    		'menu-position' => 5,
    		'exlude_from_search' => false,
    		'hierarchical' => true,
    		'menu_icon' => get_stylesheet_directory_uri() . '/images/calendar-icon.png',
    		'supports' => array('title','editor','thumbnail'),
    		'public' => true,
    		'rewrite' => array('slug' => 'event', 'with_front' => false),
    		'taxonomies' => array('post_tag', 'category')
    	);
    	register_post_type ('event', $args);
    }
    
    add_action( 'init', 'register_features' );
    
    function register_features() {
    	$labels = array(
    		'name' => 'Features',
    		'singular_name' => 'Feature',
    		'add_new' => 'Add New Feature',
    		'add_new_item' => 'Add New Feature',
    		'edit' => 'Edit Feature',
    		'edit_item' => 'Edit Feature',
    		'view' => 'View Feature',
    		'view_item' => 'View Feature',
    		'search_items' => 'Search Features',
    		'not_found' => 'No Features found',
    		'not_found_in_trash' => 'No Features found in Trash',
    	);
    	$args = array (
    		'labels' => $labels,
    		'description' => 'Features custom post type',
    		'show_ul' => true,
    		'menu-position' => 5,
    		'exlude_from_search' => false,
    		'hierarchical' => false,
    		'menu_icon' => get_stylesheet_directory_uri() . '/images/feature-icon.png',
    		'supports' => array('title','editor','thumbnail','custom-fields'),
    		'public' => true,
    		'rewrite' => array('slug' => 'features', 'with_front' => false),
    		'taxonomies' => array('feature-type', 'post_tag')
    	);
    	register_post_type ('feature', $args);
    }
    
    add_action( 'init', 'create_feature_taxonomies', 0 );
    
    function create_feature_taxonomies() {
    	// Add new taxonomy, make it hierarchical (like categories)
    	$labels = array(
    		'name' => _x( 'Feature Types', 'taxonomy general name' ),
    		'singular_name' => _x( 'Feature Type', 'taxonomy singular name' ),
    		'search_items' =>  __( 'Search Feature Types' ),
    		'all_items' => __( 'All Feature Types' ),
    		'parent_item' => __( 'Parent Feature Type' ),
    		'parent_item_colon' => __( 'Parent Feature Type:' ),
    		'edit_item' => __( 'Edit Feature Type' ),
    		'update_item' => __( 'Update Feature Type' ),
    		'add_new_item' => __( 'Add New Feature Type' ),
    		'new_item_name' => __( 'New Feature Type' ),
    	); 	
    
    	register_taxonomy( 'feature-type', array( 'feature' ), array(
    		'hierarchical' => true,
    		'labels' => $labels,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => array( 'slug' => 'feature-type' ),
    	));
    }
    
    add_action( 'init', 'register_slides' );
    function register_slides() {
    	$labels = array(
    		'name' => 'Home Slides',
    		'singular_name' => 'Home Slide',
    		'add_new' => 'Add New Home Slide',
    		'add_new_item' => 'Add New Home Slide',
    		'edit' => 'Edit Home Slide',
    		'edit_item' => 'Edit Home Slide',
    		'view' => 'View Home Slide',
    		'view_item' => 'View Home Slide',
    		'search_items' => 'Search Home Slides',
    		'not_found' => 'No Home Slides found',
    		'not_found_in_trash' => 'No Home Slides found in Trash',
    	);
    	$args = array (
    		'labels' => $labels,
    		'description' => 'Home Page Slides custom post type',
    		'show_ul' => true,
    		'menu-position' => 5,
    		'exlude_from_search' => false,
    		'hierarchical' => false,
    		'menu_icon' => get_stylesheet_directory_uri() . '/images/slide-icon.png',
    		'supports' => array('title','thumbnail', 'custom-fields'),
    		'public' => true,
    		'taxonomies' => ''
    	);
    	register_post_type ('slide', $args);
    }

    If anybody could take a look and see if anything looks weird, I would certainly appreciate it. Removing all creation of custom post types clears the error, but unfortunately, that’s not really an option. Any suggestions?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter B1gJ4k3

    (@b1gj4k3)

    Oh, and (mostly blank) staging site is here:

    http://www.theemerson.dreamhosters.com/

    Thread Starter B1gJ4k3

    (@b1gj4k3)

    Nevermind. Found it. The args in my register_slides() function had taxonomies that were blank. Instead of having

    'public' => true,
    'taxonomies' => ''

    it should just be

    'public' => true

    (for no taxonomies)

    I wanted to use a category as taxonomy, so I added this argument to my custom post type:

    'taxonomies' => 'category'

    I got the same error as above. Turns out it should’ve provide an array… Even though there is only one item. It’s logical, but can be quite frustrating when you don’t want to look up the Codex. This works:

    'taxonomies' => array('category')

    Worth adding this for reference in case anyone needs to add category box to the page:

    add_action( ‘init’, ‘add_category_to_post_types’, 11 );
    function add_category_to_post_types() { register_taxonomy_for_object_type( ‘category’, ‘POSTTYPE-NAME’ ); }

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom taxonomies generating invalid argument error (I think)’ is closed to new replies.