1

I have this error coming up on my wp_debug log file: [09-Aug-2016 11:05:05 UTC] PHP Warning: Invalid argument supplied for foreach() in ...wp-content/themes/mytheme/custom-post-types/cpts.php on line 23

I cannot seem to see why this would give that that error...

The code in question is as follows

$cpts = array(
    array('shows','Show','Shows','dashicons-tickets-alt',array('title','editor','thumbnail','comments')),
    array('people','Person','Team','dashicons-groups',array('title','editor','thumbnail')),
    array('cast','Cast & Crew','Cast & Crew','dashicons-universal-access',array('title','editor','thumbnail')),
    array('brochures','Brochure','Brochures','dashicons-book',array('title','editor','thumbnail')),
    array('jobs','Job','Jobs','dashicons-businessman',array('title','editor','thumbnail')),
    array('businesses','Business','Businesses','dashicons-building',array('title','editor','thumbnail')),
    array('prices','Price List','Price Lists','dashicons-money', array('title')),
);

function cpts_register() {

    global $cpts;

    foreach($cpts as $cpt){

        $cpt_wp_name = $cpt[0];
        $cpt_singular = $cpt[1];
        $cpt_plural = $cpt[2];
        $cpt_image = $cpt[3];
        $cpt_suports = $cpt[4];

        $labels = array(
        'name' => _x($cpt_plural, 'post type general name'),
        'singular_name' => _x($cpt_singular, 'post type singular name'),
        'add_new' => _x('Add New', $cpt_wp_name),
        'add_new_item' => __('Add New '.$cpt_singular),
        'edit_item' => __('Edit '.$cpt_singular),
        'new_item' => __('New '.$cpt_singular),
        'view_item' => __('View '.$cpt_singular),
        'search_items' => __('Search '.$cpt_plural),
        'not_found' =>  __('No '.$cpt_plural.' Found'),
        'not_found_in_trash' => __('No '.$cpt_plural.' Found in Trash'), 
        'parent_item_colon' => '',
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'show_ui' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'show_in_rest' => true,
        'supports' => $cpt_suports,
        'menu_icon'   => $cpt_image,
        );

        register_post_type($cpt_wp_name, $args );

    }

}

Thanks in advance for your help...

M

5
  • Not an answer but don't use variables in translation strings directly
    – bravokeyl
    Aug 9, 2016 at 11:40
  • @bravokeyl That's interesting. Never thought of that. I make my CPTs like this to save time. Is it just that the discovery of the strings by translation tools breaks, or is there another reason? Aug 9, 2016 at 11:55
  • @AndyMacaulay-Brook, Check this
    – bravokeyl
    Aug 9, 2016 at 11:57
  • @bravokeyl Thanks. Embarrassingly obvious when you think about it :-s Aug 9, 2016 at 12:04
  • Thanks, @AndyMacaulay-Brook I inherited this code from a previous developer - didn't know about not using the variables in translation strings.. Aug 9, 2016 at 12:31

2 Answers 2

1

Theme files are included by functions so you also need to declare global $cpts where you assign it. It won't be global automatically.

2
  • This seems to have done the trick, will have to monitor the error logs to make sure - thanks Aug 9, 2016 at 12:32
  • No problem. Glad to help. Aug 9, 2016 at 12:35
0

One fix is to check the variable’s type first

$items = $product->getFormats();

if (is_array($items)) {
    foreach ($items as $item) {
     // ...
    }
}
1
  • It's pretty clear that in this case the OP has an array though. Aug 9, 2016 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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