Home › Forums › General Issues › Select Multiple Values as meta_value
Hi I’m having trouble with a Select that has multiple values selected.
$select_categories_to_show_ids = get_field('select_categories_to_show');
$number_to_shows = get_field( 'number_to_show' );
$main_sector = get_field( 'select_main_sector' );
$args = array(
'posts_per_page' => $number_to_shows,
'posttype' => 'projects',
'orderby'=>'date',
'order'=>'DESC',
'meta_key' => 'select_main_sector',
'meta_value' => $main_sector,
'tax_query' => array(
array(
'taxonomy' => 'service_sectors',
'terms' => $select_categories_to_show_ids,
),
),
);
I need the meta_value in the following to work with any of the selected options.
But it is a foreach
<?php $select_main_sector_array = get_field( 'select_main_sector' ); ?>
<?php if ( $select_main_sector_array ): ?>
<?php foreach ( $select_main_sector_array as $select_main_sector_item ): ?>
<?php echo $select_main_sector_item; ?>
<?php endforeach; ?>
<?php endif; ?>
$select_categories_to_show_ids = get_field('select_categories_to_show');
$number_to_shows = get_field( 'number_to_show' );
$main_sector = get_field( 'select_main_sector' );
$args = array(
'posts_per_page' => $number_to_shows,
'posttype' => 'projects',
'orderby'=>'date',
'order'=>'DESC',
'tax_query' => array(
array(
'taxonomy' => 'service_sectors',
'terms' => $select_categories_to_show_ids,
),
),
);
$meta_query = array(
'relation' => 'OR'
);
foreach ($main_sector as $value) {
$meta_query[] = array(
'key' => 'select_main_sector',
'value' => '"''.$value."',
'compare' => 'LIKE'
}
}
$args['meta_query'] = $meta_query;
Thanks John,
Unfortunately this throws up the following error.
syntax error, unexpected 'foreach' (T_FOREACH)
I tried adding a ); to the line above the foreach but then the value errored
syntax error, unexpected ''.$value.'' (T_CONSTANT_ENCAPSED_STRING), expecting ')'
There is a semi-colon missing in the line abve the line with the error
Hi John,
Thank you I added a semicolon to this but it then threw up this error.
syntax error, unexpected ''.$value.'' (T_CONSTANT_ENCAPSED_STRING), expecting ')'
Code I do here is can contain some syntax errors since I don’t have a IDE telling me my mistakes. In this case there is a missing ‘ in this line value' => '"''.$value."',
it should be value' => '"''.$value.'"',
Hi John
Thank you so much for helping on this.
Unfortunately I’m still having issues with this.
I added the extra ‘ but still getting a blank page with.
PHP Parse error: syntax error, unexpected ' ' (T_STRING), expecting ')'
The topic ‘Select Multiple Values as meta_value’ is closed to new replies.
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.