The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress Multiple Instances of Widget

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #45977

    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

    Himanshu Jain

    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)
  • The forum ‘Back End’ is closed to new topics and replies.