Answer the question
In order to leave comments, you need to log in
How to correctly exclude a specific post from being displayed in WordPress code?
Good day, dear experts.
Here is the code for displaying random posts in WordPress.
<?php
$posthide = get_the_ID();
?>
<?php $news = new WP_query(); $news->query('orderby=rand&showposts=10&cat=-4,-7&post__not_in[]='.$posthide.''); ?>
<?php while ($news->have_posts()) : $news->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
...ews->query('orderby=rand&showposts=10&cat=-4,-7&post__not_in[]='.$postidid.',12,16'); ?>
<?php while ($new....
Answer the question
In order to leave comments, you need to log in
Better rewrite the code like this:
<?php
$posthide = get_the_ID();
$args = array(
'orderby' => 'rand',
'showposts' => 10,
'cat' => array(-4,-7),
'post__not_in' => array( $posthide, 12, 16)
);
$news = new WP_query(); $news->query($args);
while ($news->have_posts()) : $news->the_post();
?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question