Support

Account

Home Forums Add-ons Repeater Field If sub-field contain "xyz", show only sub-fields of the same row

Helping

If sub-field contain "xyz", show only sub-fields of the same row

  • Hi all,
    I need to create a schedule page for a web radio, simply I have a list of all my post (that is my radio show) subdivided by tab.
    Every Tab is a day of the week, so… on the tab “Monday”, I have a list of all the radio show that play Monday, etc…

    This below is an example of what I mean and how I organized the row of my repeater filed on each Radio Show page.

    Radio Show 1
    1st row => Monday and Tuesday from 10:00 to 14:00
    2nd row => Friday from 14:00 to 17:00

    Radio Show 2
    1st row => Tuesday, Wednesday, Saturday from 14:00 to 18:00
    2nd row => Thursday from 21:00 to 23:00

    Radio Show 3
    1st row => Sunday from 21:00 to 23:00.
    2nd row => Monday from 14:00 to 18:00

    To reach all this I created a repeater field (programmazione) that contains 3 subfields:
    A multiselect field to select the day or the days (giorni), a TIME field to set the start time of the radio show on that day or days (orainizio) and another TIME field to set the end time (orafine) of that day or days.
    Ok, everything works excerpt for one things.

    On my schedule page, under the radio show list of tab “Monday”, I have all the radio show that play on this day, but my code return the start time and the end time for all the row inside the query loop. This is not good.

    To better explain you I take the same example of before.
    On tab “Monday” now I see:

    Radio Show 1
    10:00 – 14:00 (THIS IS THE RIGHT START/END TIME OF THE ROW WHERE MONDAY IS SELECTED)
    14:00 – 17:00 (THIS IS WRONG, I DON’T WANT TO SEE THE START/TIME OF A DIFFERENT ROW)

    Radio Show 3

    21:00 – 23:00 (THIS IS WRONG, I DON’T WANT TO SEE THE START/TIME OF A DIFFERENT ROW)
    14:00 – 18:00 (THIS IS THE RIGHT START/END TIME OF THE ROW WHERE MONDAY IS SELECTED)

    <?php

    // filter

    date_default_timezone_set(“Europe/Rome”);
    $giorno2 = “Tuesday”;
    // filter
    function my_posts_where2( $where2 ) {

    $where2 = str_replace(“meta_key = ‘programmazione_%”, “meta_key LIKE ‘programmazione_%”, $where2);

    return $where2;
    }

    add_filter(‘posts_where’, ‘my_posts_where2’);

    $args2 = array(
    ‘cat’ => ‘palinsesto’,
    ‘post_type’ => ‘post’,
    ‘meta_query’ => array(
    ‘key’ => ‘programmazione_%_giorni’,
    ‘compare’ => ‘LIKE’,
    ‘value’ => $giorno2,
    ),

    ‘order’ => ‘DESC’,
    ‘orderby’ => ‘meta_value’,
    ‘meta_key’ => ‘programmazione_%_orainizio’,
    ‘meta_type’ => ‘TIME’

    );

    // query
    $the_query2 = new WP_Query( $args2 );
    ?>

    <?php if( $the_query2->have_posts() ): ?>
    <?php while( $the_query2->have_posts() ) : $the_query2->the_post(); ?>
    <div class=”primo”>
    <div class=”info”>
    <span class=”titolo”>“><?php the_title(); ?></span><br/>

    <?php

    $html = ”;
    ob_start();
    if(have_rows(‘programmazione’)):
    while(have_rows(‘programmazione’)): the_row();
    if (strpos(get_sub_field(‘giorni’),$giorno2) !== false):
    the_sub_field(‘orainizio’); echo ‘ – ‘; the_sub_field(‘orafine’);
    endif;
    endwhile;
    endif;
    $html = ob_get_clean();
    if ($html) {
    echo $html;
    }
    ?>

    </div>

    <div class=”secondo”>
    <div class=”cover”>“><?php the_post_thumbnail(‘thumbnail’); ?></div>
    </div>
    <?php endwhile; ?>
    <?php endif;
    wp_reset_query();
    ?>

    On the images attached you can see the structure of the Repeater field and the wrong result.

    Please, help me.
    Thanks.

  • Sorry, I put my code again on the proper way.

    <?php
    
    // filter
    
    date_default_timezone_set(“Europe/Rome”);
    $giorno2 = “Tuesday”;
    // filter
    function my_posts_where2( $where2 ) {
    
    $where2 = str_replace(“meta_key = ‘programmazione_%”, “meta_key LIKE ‘programmazione_%”, $where2);
    
    return $where2;
    }
    
    add_filter(‘posts_where’, ‘my_posts_where2’);
    
    $args2 = array(
    ‘cat’ => ‘palinsesto’,
    ‘post_type’	=> ‘post’,
    ‘meta_query’	=> array(
    ‘key’	=> ‘programmazione_%_giorni’,
    ‘compare’	=> ‘LIKE’,
    ‘value’	=> $giorno2,
    ),
    
    ‘order’	=> ‘DESC’,
    ‘orderby’	=> ‘meta_value’,
    ‘meta_key’	=> ‘programmazione_%_orainizio’,
    ‘meta_type’	=> ‘TIME’
    
    );
    
    // query
    $the_query2 = new WP_Query( $args2 );
    ?>
    
    <?php if( $the_query2->have_posts() ): ?>
    <?php while( $the_query2->have_posts() ) : $the_query2->the_post(); ?>
    <div class=”primo”>
    <div class=”info”>
    <span class=”titolo”>“><?php the_title(); ?></span><br/>
    
    <?php
    
    $html = ”;
    ob_start();
    if(have_rows(‘programmazione’)):
    while(have_rows(‘programmazione’)): the_row();
    if (strpos(get_sub_field(‘giorni’),$giorno2) !== false):
    the_sub_field(‘orainizio’); echo ‘ – ‘; the_sub_field(‘orafine’);
    endif;
    endwhile;
    endif;
    $html = ob_get_clean();
    if ($html) {
    echo $html;
    }
    ?>
    
    </div>
    
    <div class=”secondo”>
    <div class=”cover”>“><?php the_post_thumbnail(‘thumbnail’); ?></div>
    </div>
    <?php endwhile; ?>
    <?php endif;
    wp_reset_query();
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘If sub-field contain "xyz", show only sub-fields of the same row’ is closed to new replies.