How to hide DIV that contains a series of posts, if an arbitrary field is not filled in the loop?

All the best!

Tell me what I'm doing wrong?

There is a DIV block with the class "books", which is a loop to output the posts.

I want to make a check that if the value of the custom field ('value' => ') in a loop is not empty and contains the word 'Glossy', the block with the class "books" is shown, and if not filled or filled but does not contain the word 'Glossy', it is not displayed.

WP is not strong, so asked for help..

Code below:

<? 
$book_id = get_field('book_id'); // for $book_id-defined ID of the post from which to pull data
$vid_oblojki = get_field('vidaloca',$book_id);
?>

<? if (!empty($vid_oblojki['Gloss'])) : ?>
<div class="books">
 <?php 
 $posts = get_posts( array(
 'numberposts' => -1,
 'orderby' => 'date',
 'order' => 'DESC',
 'meta_query' => array(
 'relation' => 'AND', 
array(
 'key' => 'nazvaniyami',
 'value' => $book_name,
),
array(
 'key' => 'vidaloca',
 'value' => 'Glossy',
 'compare' => '=',
),
),
 'post_type' => 'books',
 ) );
 foreach( $posts as $post ){
setup_postdata($post);
?>
 <a href="<?php echo $book_link; ?>"><?php the_field('nazvaniyami'); ?></a>
 <?php 
}
wp_reset_postdata();
?>
</div>
<?php else : ?>
 <!-- Nothing is displayed -->
<?php endif ?>
April 19th 20 at 12:22
1 answer
April 19th 20 at 12:24
Solution
You have a terrible mess

1, NEVER CALL RANDOM FIELD CIRILICA!!!!! NEVER.

2, it is not clear what you have stored in the field 'vidaloca', I still think that string, then the condition would look like this

if(!empty($vid_oblojki) && mb_substr($vid_oblojki,'glossy')) :
// code
 endif;


if there is still an array, so

if ( ! empty( $vid_oblojki ) && mb_substr( implode( ", $vid_oblojki ), 'Glossy' ) ) :
 endif;


3. You fundamentally not true it is organized by field.
Kinds of covers, as I understand it a limited and preset number, then this field need to do a select with kinds of covers, and numeric values

<select name="cover_type" id="cover_type">
 <options value="">Choose cover</options>
 <options value="1">Soft</options>
 <options value="2">Glossy</options>
</select>

Then the test would look like this
if(!empty($vid_oblojki) && 2 === (int)$vid_oblojki) :
// code
 endif;

Find more questions by tags WordPress