I have two post types, courses and testimonials, sharing the same taxonomies. What I am looking to accomplish is to check whether a course is in the same category as a testimonial. Then display if it is, keeping in mind they could have more than one category each.

I am currently able to check which categories they are in using get_the_term_list() but can't figure out how to go about comparing them to one another.

What I need is to be able to check is if the categories checked off in pm_course(post-type) is the same as course_testimonials(post-type). If they have the same category then I want to display the testimonial, if not I want to do nothing.

Really a basic if else statement but I can't seem to find a way to see if the checked categories are the same in both.

  • Questions like this are usually easier to grasp if you provide some [simplified] example of actual data. – Rarst Dec 20 '13 at 23:30
  • What kind of information do you need? I'm not able to paste the plugin code here since it is quite large. – Plugin Drawer Dec 20 '13 at 23:33
  • This is how I am assigning the taxonomies to both post types ... register_taxonomy( 'course_type', array('pm_courses', 'course_testimonials'), $args ); and I am able to determine what categories are being used on each by adding echo get_the_term_list( $id, 'course_type', '', '</br>', '' ); to my single-pm_course.php page – Plugin Drawer Dec 20 '13 at 23:36
  • You should edit your question to add information. I didn't meant code, I meant human-friendly example. In line of "I have course called Dogs and testimonial Woof, I need..." – Rarst Dec 20 '13 at 23:37

Don't use get_the_term_list() as it returns an HTML string, which makes comparing more difficult.

Use wp_get_post_terms() (Codex link) instead. It can return an array, and then you can use something like array_intersect() to get the terms that match.

Your code should look something like this (I haven't tested this):

$course_terms = wp_get_post_terms($course_post_id, 'course_type'. array('fields'=>'slug'));
$testimonial_terms = wp_get_post_terms($testimonial_post_id, 'course_type'. array('fields'=>'slug')); 
$matches = array_intersect($course_terms, $testimonial_terms);
if (count($matches) > 0) echo "matching terms";
  • No matter what I do it just returns empty arrays? – Plugin Drawer Dec 21 '13 at 0:21

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.