Home › Forums › Support › PHP Warning: foreach() argument must be of type array|object in WP Show Posts
- This topic has 15 replies, 2 voices, and was last updated 3 years, 5 months ago by
David.
-
AuthorPosts
-
March 11, 2022 at 8:44 am #2150994
Juan Ramón
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
March 11, 2022 at 9:19 am #2151027David
StaffCustomer SupportHi 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 ?March 11, 2022 at 9:22 am #2151032Juan Ramón
Hi, David.
Yes, I use tags in my posts.March 11, 2022 at 10:16 am #2151115David
StaffCustomer SupportTry 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>
March 11, 2022 at 10:58 am #2151162Juan Ramón
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
March 12, 2022 at 1:54 am #2151699David
StaffCustomer SupportMade an update to the code above
March 12, 2022 at 2:39 am #2151740Juan Ramón
Good morning, David
Well, now it is. That’s perfect.
Always grateful.March 12, 2022 at 3:36 am #2151763David
StaffCustomer SupportGlad to hear that!
March 12, 2022 at 5:14 am #2151849Juan Ramón
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
March 12, 2022 at 6:00 am #2151886David
StaffCustomer SupportMade 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 anythingMarch 12, 2022 at 6:15 am #2151899Juan Ramón
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.
March 12, 2022 at 9:59 am #2152227David
StaffCustomer SupportThat one doesn’t look related. Was it there before the latest update to the code I provided ?
March 12, 2022 at 1:10 pm #2152349Juan Ramón
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.
March 13, 2022 at 5:19 am #2152743David
StaffCustomer SupportCould be related to this bug in core that’s been tracked. See here:
March 13, 2022 at 11:13 am #2153199Juan Ramón
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.
-
AuthorPosts
- You must be logged in to reply to this topic.