Support » Plugin: Widget Logic » Upgrade Error: in_array() expects parameter 2 to be array

  • Resolved noxbane


    I’ve been using this code in some widgets for about a year now with no issues:

    if ( !in_array(39,get_post_ancestors($post)) && !in_array(42,get_post_ancestors($post)) ) return true;

    And also this one:

    global $post; return (in_array(39,get_post_ancestors($post)));

    All it does is display the widget if WordPress is displaying a particular post or one of its children.

    But now I’m suddenly seeing a whole bunch of these errors at the top of every page:

    Warning: in_array() expects parameter 2 to be array, boolean given in /home/besthome/public_html/wp-content/plugins/widget-logic/widget_logic.php(284) : eval()’d code on line 1

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor alanft


    not sure why this would have changed in WP3.5. But the first bit of get_post_ancestors has this

    if ( ! $post ) return false;

    so something you could try is adding an initial test of $post in the condition like

    return ($post) && (in_array(39,get_post_ancestors($post)));

    so that if $post is false it doesn’t go on to test it against the function. just a first thought – not really thought it out.

    Hi Alan,

    Thank you very much for going above and beyond and researching this for us. I wish all plugin authors were so willing to help.

    Your fix worked great for my second example. However it doesn’t for the first.

    Specifically, changing this:

    if ( !in_array(39,get_post_ancestors($post)) && !in_array(42,get_post_ancestors($post)) ) return true;

    to this:

    if ( ($post) && !in_array(39,get_post_ancestors($post)) && !in_array(42,get_post_ancestors($post)) ) return true;

    will get rid of the error, but now that particular widget will never show at all.

    Do you have any idea why this would be?

    Plugin Contributor alanft


    do you have the “global $post” at the start?

    No I didnt, but I added it and it now works:

    global $post; if ( ($post) && !in_array(39,get_post_ancestors($post)) && !in_array(42,get_post_ancestors($post)) ) return true;

    Oddly enough, I never had it before and it worked fine before the 3.5 upgrade. Ah well, I’m just glad it works now. Many thanks.



    This really helped me today too! Just all of a sudden one of my client’s sites broke with this error. I just had to check for the array first:

    $whichPage = get_field(‘which_page?’);
    if( ($whichPage) && in_array(….

    Thanks everyone!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Upgrade Error: in_array() expects parameter 2 to be array’ is closed to new replies.