Jump to content

Get values from array to feed to wp query terms array


Recommended Posts

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.