Search code examples
phpwordpresscustom-post-typeshortcode

WP_Query for getting posts under custom taxonomy not working


I am trying to get custom post types from a custom taxonomy. Following the documentation and other similar problems related here, i did the following:

$query = new WP_Query( array(
  'post_type' => 'job',
  'tax_query' => array(

     'taxonomy' => 'location',
     'field' => 'slug',
     'terms' => 'california'
   )
)

But the problem is that this query is getting all posts, not just posts under "California" taxonomy.

If more information is needed, i can provide more code editing the question.

Thanks in advance!


Solution

  • Try something like this:

        $posts_array = get_posts(
            array(
                'posts_per_page' => -1,
                'post_type' => 'job',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'location',
                        'field' => 'slug',
                        'terms' => 'california',
              )
            )
        )
    );