Widget Logic
[resolved] Upgrade Error: in_array() expects parameter 2 to be array (6 posts)

  1. noxbane
    Posted 2 years ago #

    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


  2. alanft
    Plugin Author

    Posted 2 years ago #

    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.

  3. noxbane
    Posted 2 years ago #

    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?

  4. alanft
    Plugin Author

    Posted 2 years ago #

    do you have the "global $post" at the start?

  5. noxbane
    Posted 2 years ago #

    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.

  6. cbush57
    Posted 2 years ago #

    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!!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic