Support

Account

Home Forums General Issues Display selected relationship as a String

Solved

Display selected relationship as a String

  • Hello,

    I got a CPT called events and would like to display the name of the related artist (which I am relating with the relationship field) to the event. This is my code, in which I can’t assign the field to a variable and then include the value into the loop:

    <div class="container">
        <div class="row">  
    
         <table>
          <tr>
            <th>Artista</th>
            <th>Fecha</th>
            <th>Ciudad</th>
            <th>InformaciĆ³n</th>
          </tr>
    
          <?php
          $artista_evento = get_field('artista_del_evento');
          $today = current_time('Ymd');
    
          $args = array(
            'post_type' => 'evento',
            'post_status' => 'publish',
            'posts_per_page' => '-1',
            'meta_query' => array(
              array(
                'key' => 'fecha_del_evento',
                'compare' => '>=',
                'value' => $today,
                ),
              ),
            'meta_key' => 'fecha_del_evento',
            'orderby' => 'meta_value',
            'order' => 'ASC',
            );
    
          $query = new WP_Query($args);
          if ($query->have_posts()) :
            while ($query->have_posts()) : $query->the_post(); 
          ?>
    
          <?php $i = 0;?>
          <?php if($i == 0){ echo "<tr>"; }?>
          <td><?php echo get_the_title( $artista_evento->ID ); ?></td>
          <?php $i++;?>
          <td><?php the_field('fecha_del_evento'); ?></td>
          <?php $i++;?>
          <td><?php the_field('lugar_del_evento'); ?></td>
          <?php $i++;?>
          <td><?php echo get_the_content(); ?></td>
          <?php $i++;?>
          <?php if($i == 4){ echo "</tr>"; $i=0;}?>
        <?php endwhile; ?>
        <?php if($i < 4) echo '</tr>'; ?>
      </table>
    
      <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    </div><!-- END OF ROW -->
    </div><!-- END OF CONTAINER -->
  • You need to move the get_field into the loop. You are performing this function outside the loop so the post ID is either wrong or non-existent when you call the function.

    
    // ... code before query & loop
    
          $args = array(
            'post_type' => 'evento',
            'post_status' => 'publish',
            'posts_per_page' => '-1',
            'meta_query' => array(
              array(
                'key' => 'fecha_del_evento',
                'compare' => '>=',
                'value' => $today,
                ),
              ),
            'meta_key' => 'fecha_del_evento',
            'orderby' => 'meta_value',
            'order' => 'ASC',
            );
    
          $query = new WP_Query($args);
          if ($query->have_posts()) :
            while ($query->have_posts()) : $query->the_post(); 
    
            // put your get_field call here
            $artista_evento = get_field('artista_del_evento');
            $today = current_time('Ymd');
    
            // the rest of your template code ...
    
  • Hello John,

    I recieve this message: Trying to get property of non-object in …/wildlion/wp-content/themes/wildlion/archive-evento.php on line 71 The Faith Keepers en dondesea

    Line 71: <td><?php echo get_the_title( $artista_evento->ID ); ?></td>

  • What is get_field('artista_del_evento'); returning.

    Is this field set to return an Object or an ID?

    You can add echo $artista_evento; to test what’s being returned.

  • It was returning an ID, now I’ve changed it to return an object (in the Back-End) and also changed the code, previously I was receiving the following message:

    Trying to get property of non-object in… (same as above).

    The final code:

    $query = new WP_Query($args);
              if ($query->have_posts()) :
                while ($query->have_posts()) : $query->the_post();
              ?>
    
              <?php $i = 0;?>
              <?php if($i == 0){ echo "<tr>"; }?>
              <td>
                <?php
                 $artista_evento = get_field('artista_del_evento');
                    foreach ($artista_evento as $av) {
                        echo get_the_title( $av->ID );
                    }
                  ?>
              </td>
              <?php $i++;?>
              <td><?php the_field('fecha_del_evento'); ?></td>
              <?php $i++;?>
              <td><?php the_field('lugar_del_evento'); ?></td>
              <?php $i++;?>
              <td><?php echo get_the_content(); ?></td>
              <?php $i++;?>
              <?php if($i == 4){ echo "</tr>"; $i=0;}?>
            <?php endwhile; ?>
            <?php if($i < 4) echo '</tr>'; ?>
          </table>
    
    <?php wp_reset_postdata(); ?>
  • Did it clear up the error? is it working?

  • Sorry, yes, it is working. Thank you very much for your help.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.