Support

Account

Home Forums General Issues Querying relationship with True / False

Solved

Querying relationship with True / False

  • Hi,
    I have a post type called rto_providers which has a relationship field to select which courses they provide from another post type called courses. A list of providers will be showing on specific course pages according to the relationship selection. In case if I want to deactivate a specific provider I thought of using True/False function on rto_providers so that provider will be temporary removed from the list.

    I tried following code. But I’m getting all the providers which has a true value.

    <?php while ( have_posts() ) : the_post();
    
                $rto_providers = get_posts(array(
                  'post_type' => 'rto_providers',
                  'meta_query' => array(
                    array(
                      'key' => 'courses_offered',
                      'value' => '"' . get_the_ID() . '"',
                      'compare' => 'LIKE',
                      'key' => 'status',
                      'compare' => '==',
                      'value' => '1'
                    )
                  )
                ));
                ?>
    
                <?php if( $rto_providers ): ?>
                  <?php foreach( $rto_providers as $rto_provider ): ?>
                    <div class="row bordered-row random">
                      <div class="col-md-2 providers-list">
                        <?php echo get_the_post_thumbnail( $rto_provider->ID ); ?>
                      </div>
                      <div class="col-md-8">
                        <a href="<?php echo get_permalink( $rto_provider->ID ); ?>">
                          <?php echo get_the_title( $rto_provider->ID ); ?>
                        </a>
                      </div>
                      <div class="col-md-2">
                        <a href="<?php echo get_permalink( $rto_provider->ID ); ?>" class="btn btn-base">
                          Visit Page
                        </a>
                      </div>
                    </div>
                  <?php endforeach; ?>
                <?php endif; ?>
          <?php endwhile; ?>
  • 
    ......
                  'meta_query' => array(
                    array(
                      'key' => 'courses_offered',
                      'value' => '"' . get_the_ID() . '"',
                      'compare' => 'LIKE',
                    // added the next 2 lines
                    ),
                    array(
                      // what does this represent
                      // those that are active or inactive
                      // if you toggle the field on to make it inactive
                      // then the value here should be 0
                      'key' => 'status',
                      'compare' => '==',
                      'value' => '1'
                    )
                  )
    
  • Hi John,

    This is a bit hard to explain. However, I wanted to display all the course providers who has a true value(1) within the relationship. Instead I got all the course providers who has a true value(1) within and apart from the relationship. Sorted this out with a radio field by following code. (should be possible with TRUE/FALSE as well)

    <?php while ( have_posts() ) : the_post();
    
                $rto_providers = get_posts(array(
                  'post_type' => 'rto_providers',
                  'meta_query' => array(
                    array(
                      'key' => 'courses_offered',
                      'value' => '"' . get_the_ID() . '"',
                      'compare' => 'LIKE'
                    )
                  )
                ));
                ?>
    
                <?php if( $rto_providers ): ?>
                  <?php foreach( $rto_providers as $rto_provider ): ?>
                    <?php if( get_field('account_activation', $rto_provider->ID) == 'Active' ): ?>
                      <div class="row bordered-row random">
                        <div class="col-md-2 providers-list">
                          <?php echo get_the_post_thumbnail( $rto_provider->ID ); ?>
                        </div>
                        <div class="col-md-8">
                          <a href="<?php echo get_permalink( $rto_provider->ID ); ?>">
                            <?php echo get_the_title( $rto_provider->ID ); ?>
                          </a>
                        </div>
                        <div class="col-md-2">
                          <a href="<?php echo get_permalink( $rto_provider->ID ); ?>" class="btn btn-base">
                            Visit Page
                          </a>
                        </div>
                      </div>
                    <?php endif; ?>
                  <?php endforeach; ?>
                <?php endif; ?>
                
          <?php endwhile; ?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Querying relationship with True / False’ is closed to new replies.