Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › check if posts are in custom taxonomy
This topic contains 6 replies, has 2 voices, and was last updated by Ernest Marcinko 4 weeks, 1 day ago.
- AuthorPosts
- March 1, 2023 at 9:20 am #41578
Hi, I almost finish. Could you tell me abit about this? I want to show div in the result if posts are in custom taxonomy “topic1” (for example). I have different design to show based on custom taxonomy. In the result template, I don’t kow how to check if posts are in taxonomy “topic1”.
<?php $key = count_val'; if ($r->content_type == 'pagepost' ) { $meta_value = get_post_meta( $r->id, $key, true ); ?> <div> //show this div on the result if posts are in taxonomy "topic1" <span><?php echo $meta_value;?></span> </div> <?php } ?>
-
This topic was modified 1 month ago by
kokosan.
March 2, 2023 at 4:34 pm #41597Hi,
I am not exactly sure where this code is placed, so it highly depends. But you are using get_post_meta, which is for custom fields and not taxonomies. If you are checking for taxonomies, then maybe use the get_terms function or similar.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 2, 2023 at 4:43 pm #41602Hi, sorry for not being clear. The code below displays the div on every post. I want to display the div below only if a post has custom taxonomy “topic1”. In this case, use
$r->content_type == 'pagepost' or 'term'
?<?php
if ($r->content_type == ‘pagepost’ ) {
?>
<div>show this div on the result if posts are in taxonomy “topic1″</div>
<?php
}
?>March 2, 2023 at 5:30 pm #41606But the problem is, that posts can’t have taxonomies – posts can only have terms from custom taxonomies associated to them.
Taxonomies are the “group” for terms, for example categories are part of a taxonomy called “categories” etc..If you want to check if the post is in a specific category, then use the has_term() function. With this you can check if a post is within a specific category or any other taxonomy term.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 2, 2023 at 10:04 pm #41610I can get it done now, somehting like this
add_filter( 'asp_results', 'asp_show_element_taxonomy', 10, 1 ); function asp_show_element_taxonomy( $results ) { foreach ($results as $k=>&$r) { $extext = get_post_meta($r->id, 'example_text', true); if (has_term('', 'exampletaxonomies',$r->id)) $r->content = "<span>$extext</span><span>show this div</span>"; } return $results; }
Very awesome plugin, actually no need to create a separated custom post type to achieve this like I think before.
March 2, 2023 at 11:41 pm #41611One problem is that filter by this taxonomy is not working. I asign two posts to this taxonomy and add it to filter. When I click this filter, it show all posts, in stead of the twe posts that has this taxonomy . But, I see the two posts if use Uncategorized category filter.
March 3, 2023 at 2:08 pm #41617For the filter, try this configuration: https://i.imgur.com/Iu8w0WB.png
Best,
That should exclude posts which have not been assigned any terms from the taxonomy.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
-
This topic was modified 1 month ago by
- AuthorPosts
You must be logged in to reply to this topic.