Thank you very much for your response and your time John!
It’s a very interesting approach of the problem!
Sorry for the absence, I hope you have solved the problem !
Maybe the problem is the multi-select field … Can you paste all your query ?
I have this code example for a project if it helps (it’s not WP_Query but get_posts, seance is my custom post type, seances a repeater field and film is a relationship field) :
<?php
$today = date("Ymd");
$date = strtotime($today);
$seances = get_posts(array(
'suppress_filters' => FALSE,
'post_type' => 'seance',
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order'=>'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'seances_%_film', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
),
array(
'key' => 'date',
'compare' => '>=',
'value' => $today,
)
)
));
?>
And in functions.php
function my_posts_where( $where ) {
global $wpdb;
$where = str_replace(
"meta_key = 'seances_",
"meta_key LIKE 'seances_",
$wpdb->remove_placeholder_escape($where)
);
return $where;
}
add_filter('posts_where', 'my_posts_where');
I think compare would be LIKE and the value not in the array but ‘”85″‘ or 'value' => '"' . get_the_ID() . '"'
Have you added this code to functions.php ?
function my_posts_where( $where ) {
global $wpdb;
$where = str_replace(
"meta_key = 'authors_",
"meta_key LIKE 'authors_",
$wpdb->remove_placeholder_escape($where)
);
return $where;
}
add_filter('posts_where', 'my_posts_where');
Thanks kokers ! Your solution fix my problem :
function my_posts_where( $where ) {
global $wpdb;
$where = str_replace(
“meta_key = ‘locations_%”,
“meta_key LIKE ‘locations_%”,
$wpdb->remove_placeholder_escape($where)
);
return $where;
}
add_filter(‘posts_where’, ‘my_posts_where’);
Hi John,
Thanks for your time !
I changed my way for this, I think it was too complicated.
Now, I have two custom post types : FILM and SCHEDULE
I create my FILM with basic custom fields. And for SCHEDULE, I have two Taxonomies (Date and Location), a custom field for Time and a custom relation field for the films. So, now it’s more easy !
I have tweak an auto create title for the schedule with the custom fields. And my page display well :
DATE 1
Location 1
Film A
Film B
Location 2
Film B
Film C
DATE 2
Location 1
Film A
Film C
Everything seems to be ok for the moment with that.
Thanks, i think i’m limited in PHP to do that… I never work with save_post action and concatenation of fields. I continue to try.
Hi John,
Thank you for your replies. Despite several tests, i can’t do it. Have you a concrete example of the second method ?
Also, I have tweaked something to arrive where i want but the problem is that if the choice is not checked its value appears anyway even if there are no posts related. The result is :
DATE 1 (taxonomy)
Location 1 (taxonomy)
Film A
Film B
Location 2
Film B
Film C
Location 3
If Location 3 is empty, i don’t want to display it…
I think your method is best than mine 😉
<?php // CHAMPS LIEUX
$jours_key = "field_5886014c7f9f1";
$jours = get_field_object($jours_key);
foreach($jours['choices'] as $k => $v) :
$lieux_key = "field_5880dc1d1f784";
$lieux = get_field_object($lieux_key);
?>
<h3><?php echo $v; ?></h3>
<?php foreach($lieux['choices'] as $cle => $valeur) :
$args = array(
'post_type' => 'film',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'seances_%_jour2',
'compare' => 'LIKE',
'value' => $k,
)
)
);
$the_query = new WP_Query( $args ); ?>
<h3 style="color:#ccc"><?php echo $valeur; ?></h3>
<?php if($the_query->have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if( have_rows('seances') ): while( have_rows('seances') ): the_row();
$date = get_sub_field('jour2');
$lieux = get_sub_field('lieu');
$horaire = get_sub_field('horaire'); ?>
<?php if ($date['label'] == $v && $lieux['label'] == $valeur) : ?>
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<p><?php echo $horaire; ?></p>
<?php endif; ?>
<?php endwhile; endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
<?php endforeach; ?>
<?php endforeach; ?>
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.