Support

Account

Home Forums Front-end Issues Repeater / Select fields filled

Solving

Repeater / Select fields filled

  • Hi Elliot,

    I have a problem with the ACF plugin ..
    That is my concern. I have a custom post type named “business” and that it has a hundred shops. In every business, there are several fields including a repeater ACF field displays the tips of the trade.
    Each business can add up to 3 and it is possible that trade is not good plan.
    So what I do in my code is this:

    <ul>
    <?php shuffle(query_posts('post_type=commerce&showposts=6&orderby=rand')); ?>
        <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    
    										<?php 
    
    										$rand = rand(0,1);
    										$i = 0;
    
    										?>
    
    										<?php while(the_repeater_field('bons_plans_de_votre_magasin_commerce_detail')): ?>
    				 							
    				 							<?php if(get_sub_field('titre_commerce_detail')){ ?>
    
    										   	<?php if($rand == $i): ?>
    
    											 		
    													<li><a href="<?php the_permalink(); ?>" target="_blank"><span><?php the_sub_field('titre_commerce_detail'); ?></span></a></li>
    												
    
    												<?php endif; ?>	
    
    													
    
    											<?php } ?>
    
    										<?php endwhile; ?>
    
    <?php endwhile; endif; ?>	
    
    			</ul>

    The problem is that this code performs this action:
    6 tips I select at random and I display that contains something or not.

    How to display 6 tips filled (not empty) and if he goes on a business with no tips so he jumps up and chose another trade instead with a good plan ..

    Hoping I was clear enough … Thank you for your help 🙂

  • Hi @Anthony.C

    Sorry, I don’t understand the question. Can you please fix the indention of the code, and also re explain the question so that it is clear in english?

    Thanks
    E

  • Hi @elliot,

    Thanks to take the time to answer me.
    Here is my question :

    On the home page, I show 6 Tips randomly coming from several post (one hundred). The problem is that each post do not necessarily tips. Here is my code to display randomly.

    <ul>
    <?php shuffle(query_posts('post_type=commerce&showposts=6&orderby=rand')); ?>
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    
    										<?php 
    
    										$rand = rand(0,1);
    										$i = 0;
    
    										?>
    
    <?php while(the_repeater_field('bons_plans_de_votre_magasin_commerce_detail')): ?>
    				 							
    <?php if(get_sub_field('titre_commerce_detail')){ ?>
    
    <?php if($rand == $i): ?>
    
    											 		
    <li><a href="<?php the_permalink(); ?>" target="_blank"><span><?php the_sub_field('titre_commerce_detail'); ?></span></a></li>
    												
    <?php endif; ?>	
    
    <?php } ?>
    
    										<?php endwhile; ?>
    
    <?php endwhile; endif; ?>	
    
    </ul>
    

    The problem is that when the loop is looking for tips at random, it may choose to post item who have no tips. But she decides to show anyway.

    For example, currently on my website, I have two tips on an item and one tips on another item. But knowing that there are hundred articles and all the others have no tips while on my homepage nothing appears, you have to refresh several times to appear a good plan from time to time …

    How to tell the loop to not display a tips if it does not been found in this section and move to look for another instead.

    I hope I was clear ..

    Thank you @ elliot.

  • Hi @Anthony.C

    Can you please fix the indentation of your code?

    Your query_posts function needs to also use the meta_query param to find posts which have at least 1 repeater row of data.

    The query_posts uses the same params as WP_Query (http://codex.wordpress.org/Class_Reference/WP_Query).

    Currently, you are using a string, you will need to change this to the array format before the meta_query will work.

    Your meta_query should look like:

    
    'meta_query' => array(
           array(
               'key' => 'bons_plans_de_votre_magasin_commerce_detail',
               'value' => 1,
               'compare' => '>=',
           )
       )
    

    Good luck

    Thanks
    E

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

The topic ‘Repeater / Select fields filled’ is closed to new replies.