rhand Posted October 29, 2021 Share Posted October 29, 2021 Been stuck for a while trying to simply feed event ACF multi select category choices into a WP Query to only load custom post types with those categories frontend. The issue I am having is a PHP issue as I do not seem to be able to convert data to data that the WP Query can handle. Been using // Variables $number_of_events = get_field( 'number_of_events' ); // print_r($number_of_events); $event_object = get_field_object('events_category'); // print_r($event_object); $event_cat_name = $event_object['value']; print_r($event_cat_name); $event_cat_slug = $event_cat_name[0]->slug; // print_r($event_cat_slug); $args = array( 'post_type' => 'event', 'posts_per_page' => $number_of_events, 'event_category' => $event_cat_slug ); $the_query = new WP_Query( $args ); ?> But the issue is now that $events_cat_slug only loads the first category chosen in ACF multi select. So I was considering using $args = array( 'post_type' => 'event', 'posts_per_page' => $number_of_events, 'tax_query' => array( array( 'taxonomy' => 'event_category', 'terms' => $cat_names ) ) ); So I can add an array of terms to terms.. like `'terms' => array(cat1, cat2,)` I do get Array ( [0] => WP_Term Object ( [term_id] => 180 [name] => OnSite [slug] => onsite [term_group] => 0 [term_taxonomy_id] => 180 [taxonomy] => event_category [description] => [parent] => 0 [count] => 1 [filter] => raw [meta] => Array ( ) ) [1] => WP_Term Object ( [term_id] => 179 [name] => Virtual [slug] => virtual [term_group] => 0 [term_taxonomy_id] => 179 [taxonomy] => event_category [description] => [parent] => 0 [count] => 3 [filter] => raw [meta] => Array ( ) ) ) for current `print_r($event_cat_name);`. But how can I get all the slugs from array key 0 and array key 1 and in a way I can feed that to terms? Been trying `array_columns`: `$cat_names = array_column($event_cat_name, 'slug');` but it still prints `Array ( [0] => onsite )` with one category for example and terms chokes on it. How can I use the slug values from the arrays in `$event_cat_name` in a way terms can load it? Quote Link to comment https://forums.phpfreaks.com/topic/314139-get-values-from-array-to-feed-to-wp-query-terms-array/ Share on other sites More sharing options...
rhand Posted October 29, 2021 Author Share Posted October 29, 2021 This question might be too WordPress specific after all. So will ask at another forum focussed on WordPress and ACF. Quote Link to comment https://forums.phpfreaks.com/topic/314139-get-values-from-array-to-feed-to-wp-query-terms-array/#findComment-1591539 Share on other sites More sharing options...
requinix Posted October 29, 2021 Share Posted October 29, 2021 It's been only two hours, and it's Thursday night in the US. Be patient. It sounds like you know what you need the tax_query to be, but it also sounds like you don't quite know how $event_cat_name is formed. What is the exact value of $event_cat_name? As in if you print_r it. Quote Link to comment https://forums.phpfreaks.com/topic/314139-get-values-from-array-to-feed-to-wp-query-terms-array/#findComment-1591540 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.