Home › Forums › Add-ons › Repeater Field › repetear filter by sub_fields
Hi, i’m trying to filter a loop by two sub_fields. Apparently my code is right, but i cannot get the sub_fields to show. I will place to examples code:
As you will see in the <li>
i have to print the sub_fields: comienzo (is a picker time) and finalizacion (is a picker time) but with the first code i cannot print them. In the second code, i can print them but the arrays from args does not filter the posts.
Hope you can help me and thanks.
In the functions.php:
//filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'dia_y_horario_%", "meta_key LIKE 'dia_y_horario%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
In the template:
1- it filters but i can’t get the value form the other sub_fiels:
<?php
$args = array(
'posts_per_page' => -1, 'post_type' => 'any', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'dia_y_horario_%_dia', 'compare' => 'LIKE', 'value' => 'lunes', ), array( 'key' => 'dia_y_horario_%_lugar', 'compare' => 'LIKE', 'value' => 'Sala 1', ) ) );
$the_query = new WP_Query( $args ); ?>
<?php if( $the_query->have_posts() ): ?> <ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li class="single-event" data-start="<?php echo the_sub_field('comienzo'); ?>" data-end="<?php echo the_sub_field('finalizacion'); ?>" data-event=<?php $postType = get_post_type_object(get_post_type()); if ($postType) { echo esc_html($postType->labels->name); } ?>> <a href="<?php the_permalink(); ?>"> <em><?php the_title(); ?></em> <small><?php echo the_sub_field('lugar'); ?> | <?php get_sub_field('dia');?></small> </a>
</li>
<?php endwhile; ?> </ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
2- It does not filter, but i can get the value form the other subfiels:
<?php
$args = array( 'category_name' => 'lunes', 'post_type' => 'any', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'dia_y_horario_%_dia', 'value' => 'lunes', 'compare' => 'LIKE' ), array( 'key' => 'dia_y_horario_%_lugar', 'value' => 'Sala 2', 'compare' => 'LIKE' ) ) ); $sala2 = new WP_Query($args); while($sala2->have_posts() ): $sala2->the_post(); ?> <?php if ( have_rows('dia_y_horario') ) : while ( have_rows('dia_y_horario') ) : the_row(); ?> <li class="single-event" data-start="<?php the_sub_field('comienzo') ?>" data-end="<?php the_sub_field('finalizacion') ?>" data-event="<?php $postType = get_post_type_object(get_post_type()); if ($postType) { echo esc_html($postType->labels->name); } ?>"> <a href="<?php the_permalink(); ?>"> <em class="event-name"><?php the_title(); ?></em> <small><?php the_sub_field('lugar'); ?> <br> <?php the_sub_field('dia');?></small> </a>
</li>
<?php endwhile; ?> <?php endif; ?> <?php endwhile; wp_reset_postdata(); ?>
the_sub_field() will not do anything unless it is in a have_rows() loop https://www.advancedcustomfields.com/resources/have_rows/
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.