0

I'm working on a portfolio with filters based on custom post type. That filters must display only categories for the posts that are displayed in the portfolio, since user can specify them by id in the shortcode - [portfolio ids="1,2,3"] And I can't get a list of that categories ids.

Here is a simple example of what I'm trying to do:

A list of custom post ids from the shortcode:

$ids

An array of the same ids:

$id_array = explode(',', $ids)

A var of interest that must return a list of categories ids:

$cat_ids = ???  

Next we get only categories with needed id:

$categories = get_categories(
        array(
            'include' => $cat_ids,
            'taxonomy' => 'portfolio_category'
        )
    );

And finally have our categories displayed:

echo '<ul>';
        echo '<li><a href="#" data-filter="isotope-item" class="active">all</a></li>';
        foreach ($categories as $category) {
            if( isset( $category->term_id ) ){
                echo '<li><a href="#" data-filter="'.$category->category_nicename.'">'.$category->cat_name.'</a></li>';
            }
        }
    echo '</ul>';

The cloasest what i get was:

$cat_ids = wp_get_object_terms( $ids, 'portfolio_category', array('orderby' => 'name', 'fields' => 'ids') );

But it returns only one id

0

Ok, I thought I was out of options after few hours spent on this little piece of code. But somehow I managed to get result, just after I hopelessly posted the question here.

$terms = wp_get_object_terms( $id_array, 'portfolio_category' );

$new_array = array();
foreach($terms as $key => $term) {
    $new_array[$key] = $term->term_id;
}

$cat_ids = implode(',', $new_array);

$categories = get_categories(
    array(
        'include'                  => $cat_ids,
        'taxonomy'                 => 'portfolio_category'
    )
);

Maybe somebody could comment on this solution?

  • Please accept your own answer ;-) – Pieter Goosen Jul 16 '15 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.