Y
Y
Ysery2020-05-12 14:53:24
WordPress
Ysery, 2020-05-12 14:53:24

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; ?>


I edited it like this:

...ews->query('orderby=rand&showposts=10&cat=-4,-7&post__not_in[]='.$postidid.',12,16'); ?>
<?php while ($new....


Hiding from the issuance of 12 and 16 post (record). But the code does not seem to work, at least on the root page of the site I see excluded posts in the search results. How to exclude the 12th and 16th post?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-05-13
@Ysery

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; ?>

I'm a bit unsure that cat needs an array, if there is an error, try just -4,-7 there.
So the code is clearer and easier to write than trying to find errors with quotes in the string, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question