Problems with a Query in wordpress

1

I'm doing a function to bring some courses for that I have a post type where I charge the courses and a taxonomy of marks that I assign to which course they belong to show this I have a section that is a filtering where I have to tell you which courses are find active, for that create a field customs fields selector to put that course is enabled / not enabled. The problem is that when I enable a course from backend, it does not show it to me.

The example can be seen here also attached a photo of how is the filter

The query is as follows;

<div class="col-md-12 text-center">
  <div id="filter" class="animated fadeInDown bottomspace10">
    <ul>
      <li><a href="#" data-filter="*" class="selected"><i class="icon icon-reorder"></i> Ver Todos</a></li>

      <li><a href="#" data-filter=".activo" title="¡Fechas Definidas!"><i class="icon icon-th"></i> Cursos Activos</a></li>
      <li>Líneas:</li>

      <?php 
									$terms = get_terms('categoria-cursos', get_queried_object());
									if($terms){

									foreach ($terms as $termino){ ?>
      <li>
        <a href="#" data-filter=".id<?php echo $termino->term_id;?>"><i class="icon icon-th"></i> 
											<?php echo $termino->name; ?>
											</a>
      </li>

      <?php } }?>
    </ul>
  </div>
</div>

<div id="content">
  <?php if($terms){ //si existe contenido 

					foreach($terms as $termino){
						$args = array(
							'post_type' => 'curso',
							'showposts'=> -1,
							'tax_query' => array(
						    array(
						        'taxonomy' => 'categoria-cursos',
						        'field' => 'id',
						        'terms' => $termino->term_id
						        )
						    )
						);
						$posts = new WP_Query( $args );
					

							if($post){
								while($posts->have_posts()):
									$posts->the_post();
								$cur = $post;

									?>
  <div class="boxportfolio1 bp-height item id<?php echo $termino->term_id;?> <?php  if(get_terms('curso_activo',$cur->ID) == 'habilitado'){echo 'activo';}?>">
    <!-- desde acá parte un bloque -->
    <div class="boxcontainer">
      <div class="prod-imagecontainer">
        <img src="<?php echo get_the_post_thumbnail_url($cur->ID,'thumbnail'); ?>" alt="<?php echo $cur->post_title; ?>">
      </div>
      <div class="roll">
        <div class="wrapcaption">
          <a href="<?php echo get_the_permalink($cur->ID); ?>"><i class="icon-arrow-right captionicons"></i></a>
        </div>
      </div>
      <div class="prod-infocontainer">
        <h1>
          <a href="vero-alphacam-router.html">
            <?php echo $cur->post_title; //este es tl titulo?>
          </a>

        </h1>
        <p>
          <?php echo get_the_content($cur->ID);?>
        </p>
      </div>
    </div>
  </div>
  <?php endwhile;
							}
						?>
  <?php } } ?>

</div>
    
asked by MarianoF 16.04.2018 в 20:06
source

2 answers

1

You are using get_terms () to get to the value of custom field where you store if a course is active or not. The problem is that this function returns a% co_of% results and therefore you can not compare the obtained result as if it were a array using the operator string .

An alternative could be using the get_post_meta () function by sending the third parameter as == returns a single value in true format.

Try changing this line:

<?php 
     if (get_terms('curso_activo',$cur->ID) == 'habilitado') {
     echo 'activo';
     } 
   ?>

For this:

<?php 
 if (get_post_meta('curso_activo', $cur->ID,true) === 'habilitado') {
     echo 'activo';
  } 
?>

Finally, and as an addition, I advise you to develop in debug mode

    
answered by 17.04.2018 / 11:10
source
1

I see a bit of strange use of get_terms try this:

<?php 
  $ct = get_terms([
    'taxonomy' => 'curso_activo',
    'hide_empty' => true,
  ]); 
  // var_dump($ct); <-- descomentar para ver que devuelve get_terms
  if (!empty($ct)&&in_array('habilitado', $ct))
  {echo 'activo';}
  ?>">
    
answered by 16.04.2018 в 22:20