Support » Plugins and Hacks » Excluding multiple posts from a widget

  • Resolved safin

    (@safin)


    Hello,
    I have a custom skin on my wordpress blog which has two containers for posts. My aim is to query the same categories but not show posts pulled in the first container call.

    Here is a exclude code i added in the first container

    <?php $getposts = new WP_query(); $getposts->query('showposts='.$options['leftpostnum'].'&amp;cat='.$section); ?>
    		<?php global $wp_query; $wp_query->in_the_loop = true; ?>
    		<?php while ($getposts->have_posts()) : $getposts->the_post(); ?>
    		<?php $do_not_duplicate[] = $post->ID; ?>
                    ....................
                    Come html Code there
                    .....................
    		<?php endwhile; ?>

    Basically $do_not_duplicate is an array with all post id’s

    Now in the second block i am adding this to the query using the wordpress function post__not_in

    <?php $getposts = new WP_query(); $getposts->query('showposts=10&amp;offset='.$options['rightpostnum'].'&amp;cat='.$section.'&amp;post__not_in='.$do_not_duplicate); ?>
    		<?php global $wp_query; $wp_query->in_the_loop = true; ?>
    		<?php while ($getposts->have_posts()) : $getposts->the_post(); ?>
    		<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    		<?php endwhile; ?>

    Unfortunately that doesn’t seem to work. This is the error i get.

    Warning: array_map() [function.array-map]: Argument #2 should be an array in /home/xyz/public_html/wp-includes/query.php on line 1725

    Warning: implode() [function.implode]: Invalid arguments passed in /home/xyz/public_html/wp-includes/query.php on line 1725

    The wordpress verion is 2.7.1.

    I used vardumps to check and the array is there before second query executes. However in the query file throws an error.

    Any help.
    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try:

    $args = array(
        'showposts' =>10,
        'offset' => $options['rightpostnum'],
        'cat' => $section,
        'post__not_in' => array($do_not_duplicate)
    	);
    $getposts = new WP_query(); $getposts->query($args);

    Hi,
    That didn’t work. Adding an array() makes it multi dimensional and although the error message is not shown, it basically does nothing also.
    See the query that executes.

    Time: 0.000117063522339
    Query: SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) WHERE 1=1 AND wp_posts.ID NOT IN (1) AND wp_term_taxonomy.taxonomy = 'category' AND wp_term_taxonomy.term_id IN ('56', '92', '93') AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 3
    Call from: require, require_once, include, WP_Query->query, WP_Query->get_posts

    Notice the wp_posts.ID NOT IN (1) part.

    I did a print_r immediately after the args

    $args = array(
        'showposts' => $options['rightpostnum'],
        'cat' => $section,
        'post__not_in' => array($do_not_duplicate)
    	 );
    	 print_r(array($do_not_duplicate));

    And i get this
    Array ( [0] => Array ( [0] => 335 [1] => 295 [2] => 285 [3] => 275 [4] => 241 [5] => 231 [6] => 305 [7] => 147 [8] => 140 [9] => 125 [10] => 122 [11] => 111 [12] => 106 [13] => 96 [14] => 80 ) )
    Notice the double array

    You are right I didn’t confirm the results, but this worked

    $args = array(
      'post__not_in' => $do_not_duplicate
    	);

    Note that I can’t confirm what your $section value is.

    Thanks, that did it.

    For future, any particular reason why the first method was not working? And this works.

    Can’t say, just had better luck using the argument array method.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Excluding multiple posts from a widget’ is closed to new replies.