Site logo

Archived topic

PHP Warning: foreach() argument must be of type array|object in WP Show Posts

15 replies · Started by Juan Ramón on March 11, 2022

Viewing posts 1–15 of 16

Hello again friends.

I need a help or hints to fix a bug.

I am using the WP Show Posts plugin for related posts, I added it in a hook with this code:

Screenshot: https://ibb.co/h1Fwhc4

<div class="wpsp-related-posts1  grid-container">
	<h2>Artículos relacionados</h2>
<?php    
if ( is_single() ) {
        $tags =  get_the_tags();
        $tags_list = [];
        foreach ($tags as $tag)
            $tags_list[] = $tag->slug;
        $tag_string = implode( ', ', $tags_list);
    } else {
        $tag_string = get_tag( get_query_var( 'tag' ) );
    }

    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' );
    ?>
</div>

Well, it works correctly, but in error_log I get this warning:

[11-Mar-2022 15:24:01 UTC] PHP Warning: foreach() argument must be of type array|object, bool given in ****/public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code on line 7

I know that soon WP Show Posts plugin will be integrated in GenerateBlocks PRO (I wait impatiently :P) but could the supposed problem with foreach be solved in the meantime? (I'm using PHP 8.1)

Thanks

Hi there,

so that code will filter the list by posts that have the same taxonomy Tag as the current post.
Are you using Tags on your posts ?

Hi, David.
Yes, I use tags in my posts.

Try changing the snippet to this, where we check to make sure there are tags:

<div class="wpsp-related-posts1  grid-container">
	<h2>Artículos relacionados</h2>
<?php
if ( is_single() && function_exists( 'wpsp_display' ) ) {
        $tags =  get_the_tags();
        $tag_string  = '';
        if ( !empty($tags) ) {
            $tags_list = [];
            foreach ($tags as $tag)
                $tags_list[] = $tag->slug;
            $tag_string = implode( ', ', $tags_list);
        }
    } else {
        $tag_string = get_tag( get_query_var( 'tag' ) );
    }

    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' );
    ?>
</div>

Great, David

Right, it's possible that some old posts didn't have tags.

This Warnings are no longer generated.

Very kind, as always. Appreciated.

But now i have another different warning XD

[11-Mar-2022 17:59:11 UTC] PHP Warning: Undefined variable $tag_string in /public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code on line 17

Good morning, David
Well, now it is. That's perfect.
Always grateful.

Glad to hear that!

Hello again, David

I don't want to bother too much with this issue, maybe it would be good to wait for the imminent integration of WP Show Posts in GenerateBlocks PRO, anyway, for information purposes there goes a new bug that has appeared:

[12-Mar-2022 11:10:02 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function wpsp_display() in  /public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code:18
Stack trace:
#0  /public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(215): eval()
#1  /public_html/wp-includes/class-wp-hook.php(307): GeneratePress_Hook->execute_hook()
#2  /public_html/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters()
#3  /public_html/wp-includes/plugin.php(474): WP_Hook->do_action()
#4  /public_html/wp-content/themes/generatepress/content-single.php(99): do_action()
#5  /public_html/wp-includes/template.php(772): require(' /...')
#6  /public_html/wp-includes/template.php(716): load_template()
#7  /public_html/wp-includes/general-template.php(204): locate_template()
#8  /public_html/wp-content/themes/generatepress/inc/theme-functions.php(572): get_template_part()
#9  /public_html/wp-content/themes/generatepress/single.php(29): generate_do_template_part()
#10  /public_html/wp-includes/template-loader.php(106): include(' /...')
#11  /public_html/wp-blog-header.php(19): require_once(' /...')
#12  /public_html/index.php(17): require(' /...')
#13 {main}
  thrown in  /public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code on line 18

I insist, if it does not pose a security risk (I guess not) the final operation of the related posts is correct, so I am in no hurry to patch it.

Regards

Made another edit to the code above to check function_exists( 'wpsp_display' ) - that way if your hook gets called before WPSP is loaded it won't do anything

Hi, David
I already applied the change, when I get some more visits I will see if it generates any other errors.

At the moment there is only one new one (which I don't know if it can be related)

[12-Mar-2022 13:08:17 UTC] PHP Warning: Attempt to read property "post_type" on null in public_html/wp-includes/class-wp-query.php on line 4273

Nice support, thanks again.

That one doesn't look related. Was it there before the latest update to the code I provided ?

Yes, it appeared right after the last update of your code, but it may be due to something else, this new error appears much less often in the log than the previous ones, which makes me think that it is caused by something else.

Thanks for the tip, David. I'll keep an eye on it.

I've allowed some time for the log to record some more errors, and although there are fewer now, they all seem to be related to the PHP version. Downgrading to 8.0 and 7.4 makes most of them disappear (I'm now on 8.1).

Among all the errors that remain only appears a few times this one related to the related posts / GP.

[12-Mar-2022 21:04:39 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function wpsp_display() in /public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code:18

Stack trace:
#0   public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(215): eval()
#1   public_html/wp-includes/class-wp-hook.php(307): GeneratePress_Hook->execute_hook()
#2   public_html/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters()
#3   public_html/wp-includes/plugin.php(474): WP_Hook->do_action()
#4   public_html/wp-content/themes/generatepress/content-single.php(99): do_action()
#5   public_html/wp-includes/template.php(772): require('  ...')
#6   public_html/wp-includes/template.php(716): load_template()
#7   public_html/wp-includes/general-template.php(204): locate_template()
#8   public_html/wp-content/themes/generatepress/inc/theme-functions.php(572): get_template_part()
#9   public_html/wp-content/themes/generatepress/single.php(29): generate_do_template_part()
#10  public_html/wp-includes/template-loader.php(106): include('  ...')
#11  public_html/wp-blog-header.php(19): require_once('  ...')
#12  public_html/index.php(17): require('  ...')

#13 {main}
  thrown in   public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code on line 18

The rest of errors are related to the Rank Math plugin or others without reference that I recognize. In any case, it does not seem to be serious because nothing seems to be malfunctioning.

The good news is that there are no new errors since 16:00 today.

This archived topic is closed to new replies.