0

I have used this technique before and cannot work out why this time it is not working.

'news_category' is a custom taxonomy of my custom post type 'News'.

I am getting the taxonomy of the current post (which is custom post type 'news') using get_the_term_list, replacing the spaces with '-' and using the variable in my query.

If I echo the query it gives me the exact string I am expecting - and if I manually enter this string into the query, it works, but the variable doesnt.

<?php $cats = get_the_term_list($post->ID, 'news_category'); ?>

                <?php $cat = str_replace(' ', '-', $cats);?>

                <?php $category = strtolower($cat); ?>

                <?php echo $category; ?>

                    <?php $args = array(

                                        'post_type' =>'news',
                                        'news_categories' => $category,
                                        'post__not_in'=> array($post->ID)
                                        ); ?>



                    <?php $sidebar_category = new WP_Query($args); ?>
0

You have to use array like this:

array(
      'post_type' =>'news',
      'category_name' => $category,  //use category slug (NOT name).
      'post__not_in'=> array($post->ID)
     );

For more reference please visit this link

0

The variable is not working because get_the_term_list returns a string of HTML links, whereas your $args array requires slugs. I'm not sure if you can pass an array of slugs using your current method or not. You can certainly pass an array of slugs using a tax_query array.

Incidentally, using your current method, you should be setting the tax name to 'news_category', not 'news_categories', but this is moot.

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.