Treehouse: Grow your CSS skills. Land your dream job.

WordPress Multiple Instances of Widget

  • # June 30, 2013 at 11:06 am

    I made a custom widget for displaying post excerpts in the sidebar. I want to have multiple instances of the widget, but it’s showing posts more than once.

    I tried to make an array to collect the post IDs then use the ‘post__not_in’ in the widget query, but that didn’t work.

    This is in the widget’s code

    global $do_not_duplicate,
    $query = new WP_Query(array(
    ‘post_type’ => $post_type,
    ‘post__not_in’ => $duplicate_posts,
    ‘orderby’ => ‘rand’
    if (have_posts()) : while ($query->have_posts()) : $query->the_post();
    $do_not_duplicate[] = $post->ID;

    Then on the page template

    $do_not_duplicate = array();

    If I do a var_dump on the widgets it only has the current ID and the previous. So the first widget instance has the first post ID, the second one has the first and second ID, and so on.

    It seems like I need to have the ID before the query so it can check against the array, but how do you do that.

    Also, I’m not sure if it’s related but random doesn’t work the first instance of the widget. It just queries the latest post.

    I found this solution from here

    # February 4, 2015 at 11:06 pm

    Hello gilgimech,

    I am stuck in same situation and not able to find any solutions.

    Did you find some solution for this?

    Thanks & Regards

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.