cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
280 stars 82 forks source link

PHP Warning: Invalid argument supplied for foreach() #246

Closed ghost closed 4 years ago

ghost commented 4 years ago

Describe the bug I am getting an error in php on line ( foreach ($post_tags as $tag) )

PHP Warning:  Invalid argument supplied for foreach() in /home/myusername/public_html/wp-content/plugins/custom-popular-posts/custom-popular-posts.php on line 31

To Reproduce

function ahpp_shortcode($atts)
{

   // Get popular posts
    $args = shortcode_atts(array(
    'limit' => 2,
    'range' => 'last7days'), $atts);

    $popular_posts = new WPP_Query(array ('limit' => $args['limit'], 'range' => $args['range']));

    if ($popular_posts->get_posts()) {
        $tags = array();

        foreach ($popular_posts->get_posts() as $popular_post) {
            $post_tags = get_the_tags($popular_post->id);

            foreach ($post_tags as $tag) {
                if (! isset($tags[$tag->slug])) {
                    $tags[$tag->slug] = array(
                    'id' => $tag->term_id,
                    'name' => $tag->name,
                    );
                }
            }
        }

        ob_start();
        foreach ($tags as $each_tag) {
            echo'<a style="margin-right: 20px;" class="btn button ahpp" href=" '. get_tag_link($each_tag["id"]) .' "> '. $each_tag["name"] .'</a>';
        }
        //var_dump($tags);
        return ob_get_clean();
    }
}
add_shortcode('ahpp-tags', 'ahpp_shortcode');

Screenshots If applicable, add screenshots to help explain your problem.

Environment:

cabrerahector commented 4 years ago

Unfortunately you'll have to reach out to whoever coded that because the plugin you're using is not the official WordPress Popular Posts plugin:

/wp-content/plugins/custom-popular-posts/custom-popular-posts.php

ghost commented 4 years ago

this is the code you gave me to display popular post tags

I updated and posted the whole code with shortcode

cabrerahector commented 4 years ago

Is that so? I don't remember ever writing that so I'm curious, mind telling me when exactly I did that?

Also, and assuming I actually wrote that code, you'll need to share the entire content of the plugin (not just that code snippet) if you want to get any help from me. What you posted isn't enough to tell what's going on.

ghost commented 4 years ago

I updated, I this something to do with PHP 7.3?

cabrerahector commented 4 years ago

No, it's not that. I believe that one of your popular posts has no tags assigned to it which would explain why PHP complains about $post_tags not being a valid foreach resource.

Try this:

$post_tags = get_the_tags($popular_post->id);

if ( ! $post_tags || is_wp_error($post_tags) ) {
    continue;
}

// rest of the code continues below