phpwordpresspost-meta

Illegal string offset on meta value === 1


I want o display post from custom post after checking if the meta key value of the post is === 1. But I get an error "legal string offset 'isAirConditioning'". What did I do wrong?

I have created a custom post called "Works". To this custom post I have made a meta box which allows me to mark which work will be assigned to the air conditioning, refrigeration or recovery. Then on the page template I'm running an if statement to check on what page I'm currently on to display the correct works. If I'm on the air conditions works page then a query is passed to get all the post with meta from this custom post, then a second if statement is made to check if the $meta['isAirConditioning'] === '1'.

 <?php
    $classes = get_body_class();
    if(in_array('page-id-233', $classes)) {
      echo '<p>do something</p>';
    }
    elseif(in_array('page-id-239', $classes)) {
      echo '<ul>';
       $args = array('post_type' => 'works', 'orderby' => 'date', 'order' => 'ASC', 'showposts' => 100);
       $the_query = new WP_Query($args);

       while ($the_query->have_posts() ) : $the_query->the_post();

       $meta = get_post_meta( $post->ID, 'portfolio_details', true );

       if ($meta['isAirConditioning'] === '1') {
  ?>
         <li class="works-wrapper col-3 col-sm-3 col-md-3 col-lg-3">
            <a class="works-img"href="<?php the_permalink() ?>" >
              <span class="works-gradient"></span>
              <?php the_post_thumbnail(); ?>
             </a><!-- .works-img -->
            <a class="works-title" href="<?php the_permalink() ?>" ><?php the_title(); ?></a>
         </li><!-- .worsk-wrapper -->

  <?php
   }
      endwhile;

      echo '</ul>';
  }
  elseif(in_array('page-id-241', $classes)) {
    echo '<p>do something</p>';
  }
 ?>

The post are being displayed but I get an error "Illegal string offset 'isAirConditioning'"


Solution

  • I managed to solve the problem by checking if the $meta is empty or not

    if(!empty($meta)) :
       if ($meta['isAirConditioning'] === '1') {
       ?>
         <li class="works-wrapper col-3 col-sm-3 col-md-3 col-lg-3">
           <a class="works-img"href="<?php the_permalink() ?>" >
             <span class="works-gradient"></span>
             <?php the_post_thumbnail(); ?>
           </a><!-- .works-img -->
           <a class="works-title" href="<?php the_permalink() ?>" ><?php the_title(); ?></a>
         </li><!-- .worsk-wrapper -->
       <?php
      }
      endif;