1

I'm trying to pull custom post types that have a specific custom taxonomy

This is what i've tried:

$args = array(
            'posts_per_page'    => -1,
            'post_type'         => 'products',
            'post_status'       => 'publish',
            'tax_query'         => array(
                array(
                    'taxonomy'  => 'subjects',
                    'field'     => 'name',
                    'terms'     => $shortcode_atts['subject'],
                ),
                array(
                    'taxonomy'  => 'plans',
                    'field'     => 'name',
                    'terms'     => $shortcode_atts['plan'],
                ),
            ),
        );

$shortcode_atts looks like this:

string(80) "Array
(
    [subject] => אינטרנט
    [plan] => מסלול 10 גיגה 
)
"

i.e. That's in Hebrew, subject=internet and plan=10giga

I also tried to use operator, wasn't sure in which place to add it so I tried in all locations but it didn't work. Can someone guide me how to pull these posts?

This query returns all cpt's without the filtering that I want.

EDIT: shortcode_atts is this:

function show_product( $atts, $content = null, $tag ) {
    $shortcode_atts = shortcode_atts( array(
        'subject' => '',
        'plan'  =>  ''
    ), $atts );
}
add_shortcode( 'show_product', 'show_product' );

Thanks!

6
  • Your question says $shortcode_atts is a string. It should be an array. Can you include more of where you're setting it. Jul 2 '18 at 12:06
  • I edited the post, do I need to set the values to $atts['subject'] and $atts['plan']? the documentation for this method isn't so good.
    – odedta
    Jul 2 '18 at 12:08
  • No, what you have is correct. Can you var_dump( $args ) and show what you get. Jul 2 '18 at 12:17
  • pastebin.com/h3m64HeZ - It's supposed to get only post 1 and 2, not 0.
    – odedta
    Jul 2 '18 at 12:27
  • No, dump $args. Just the arguments, before you do the query. Jul 2 '18 at 12:31
1

You use multiple taxonomies at once, so use'relation' => 'AND':

<?php

$args = array(
    'posts_per_page'    => -1,
    'post_type'         => 'products',
    'post_status'       => 'publish',
    'tax_query'         => array(
        'relation' => 'AND', // this is what was missing
        array(
            'taxonomy'  => 'subjects',
            'field'     => 'name',
            'terms'     => $shortcode_atts['subject'],
        ),
        array(
            'taxonomy'  => 'plans',
            'field'     => 'name',
            'terms'     => $shortcode_atts['plan'],
        ),
    ),
);
9
  • That actually doesn't find any post, which is weird because your syntax looks correct.
    – odedta
    Jul 2 '18 at 12:44
  • Try to cast 'terms' like 'terms' => (string) $shortcode_atts['plan'].
    – Max Yudin
    Jul 2 '18 at 12:50
  • It's a string originally, anyhow it doesn't work.
    – odedta
    Jul 2 '18 at 12:51
  • This is how everything looks: pastebin.com/HyAb9f34
    – odedta
    Jul 2 '18 at 12:54
  • How did you get it?
    – Max Yudin
    Jul 2 '18 at 12:57

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.