I'm trying to pass an array to the terms parameter. Basically, I have a 'portfolio' custom post type where I need to get all the categories and pass them to the terms in order to only show that 'filtered' results, please, have a look at the code below:
if ( ! empty ( $categories ) ) {
foreach ( $categories as $cat ) {
$filter = get_term_by( 'id', $cat, 'portfolio-category' );
if ( ! empty( $filter ) ) {
$query_filter .= $filter->slug . ',';
}
}
}
$args = array(
'post_type' => 'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'field' => 'slug',
'terms' => $query_filter,
)
),
'paged' => $paged,
'posts_per_page' => $projects_per_page,
);
The problem seems to happen because of the array format that 'terms' get. If I pass in terms an array directly, like this:
'terms' => array('one', 'two', 'three'),
It does work, but the result I get from $query_filter, no matter if I format it first with str_replace (since I get the same result 'one', 'two', 'three'), it does not work, I guess because of the array format.
Maybe some of you can help me with this since it seems a very basic lack in my PHP logics where I'm missing in.
Many thanks!
I'm trying to pass an array to the terms parameter. Basically, I have a 'portfolio' custom post type where I need to get all the categories and pass them to the terms in order to only show that 'filtered' results, please, have a look at the code below:
if ( ! empty ( $categories ) ) {
foreach ( $categories as $cat ) {
$filter = get_term_by( 'id', $cat, 'portfolio-category' );
if ( ! empty( $filter ) ) {
$query_filter .= $filter->slug . ',';
}
}
}
$args = array(
'post_type' => 'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'field' => 'slug',
'terms' => $query_filter,
)
),
'paged' => $paged,
'posts_per_page' => $projects_per_page,
);
The problem seems to happen because of the array format that 'terms' get. If I pass in terms an array directly, like this:
'terms' => array('one', 'two', 'three'),
It does work, but the result I get from $query_filter, no matter if I format it first with str_replace (since I get the same result 'one', 'two', 'three'), it does not work, I guess because of the array format.
Maybe some of you can help me with this since it seems a very basic lack in my PHP logics where I'm missing in.
Many thanks!
Share Improve this question asked Jul 1, 2019 at 5:43 Urko.GrrtUrko.Grrt 32 bronze badges1 Answer
Reset to default 0The problem is that you're passing a string with commas in it, not an actual array.
The result of:
foreach ( $categories as $cat ) {
$filter = get_term_by( 'id', $cat, 'portfolio-category' );
if ( ! empty( $filter ) ) {
$query_filter .= $filter->slug . ',';
}
}
Is:
'one,two,three,'
Not an array. So you're querying for portfolio items that are in the "one,two,three," category, which presumably doesn't exist.
If you want to query multiple slugs, you need to pass an array, and to do that you need to build an array, which would look like this:
$query_filter = []; // Initialise empty array.
foreach ( $categories as $cat ) {
$filter = get_term_by( 'id', $cat, 'portfolio-category' );
if ( ! empty( $filter ) ) {
$query_filter[] = $filter->slug; // Add slug to array.
}
}
However. None of this is even necessary. I can see from your code that $categories
is already an array of IDs. This means you can just pass $categories
to the query directly. There's no need to bother getting the slugs:
$args = array(
'post_type' => 'portfolio',
'tax_query' => array(
rray(
'taxonomy' => 'portfolio-category',
'field' => 'term_id',
'terms' => $categories,
),
),
'paged' => $paged,
'posts_per_page' => $projects_per_page,
);