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,
    $post;
    $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 http://wordpress.org/support/topic/do-not-duplicate-post-covering-2-queries

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".