$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Multiple loops without repeating content|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Multiple loops without repeating content

matteradmin10PV0评论

I have a second loop on a page where I want to display posts from the same category as the present post but excluding the present post.

I have a loop that displays all the posts from the same category as the current post, but does not exclude the current post.

  <?php
  $project_category = wp_get_post_categories($post->ID); 
  $postid = $post->ID;
  ?>

  <?php
  $the_query = new WP_Query( array(
      'category__in' => $project_category,
      'posts_per_page' => -1,
      'post__not_in' => $postid,
  ) );
  ?>

  <?php                                                      
  $loop = new WP_Query( $the_query );
  while ( $loop->have_posts() ) : $loop->the_post();  
  ?>

I have a second loop on a page where I want to display posts from the same category as the present post but excluding the present post.

I have a loop that displays all the posts from the same category as the current post, but does not exclude the current post.

  <?php
  $project_category = wp_get_post_categories($post->ID); 
  $postid = $post->ID;
  ?>

  <?php
  $the_query = new WP_Query( array(
      'category__in' => $project_category,
      'posts_per_page' => -1,
      'post__not_in' => $postid,
  ) );
  ?>

  <?php                                                      
  $loop = new WP_Query( $the_query );
  while ( $loop->have_posts() ) : $loop->the_post();  
  ?>
Share Improve this question edited Jul 21, 2015 at 2:15 gmazzap 46.3k6 gold badges95 silver badges147 bronze badges asked Jul 21, 2015 at 2:04 scottayscottay 234 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

As explained in Codex

post__not_in (array) - use post ids. Specify post NOT to retrieve.

post__not_in argument have to be passed as array.

Change your query to:

$the_query = new WP_Query( array(
    'category__in'   => $project_category,
    'posts_per_page' => -1,
    'post__not_in'   => array( $postid ),
) );

And it should work.

Post a comment

comment list (0)

  1. No comments so far