Home › Forums › General Issues › Custom Post type taxonomies
My Custom Post type taxonomies are not displaying like the regular blog tags and categories are.
I have created a custom field with the field type, “taxonomy” and would like to use it to: Select the category or tag that you would like to appear in a particular page’s sidebar.
I have it working fine for the built in taxonomies (category and tag) for wordpress, but I can’t seem to figure out why it’s not working for the custom post type taxonomies.
Here is my code:
function posts(){
$categoryValue = get_field('recent_posts');
if($categoryValue){ //check if a category is selected in the post
$args=array(
'cat' => $categoryValue,
'posts_per_page'=>1, // Number of related posts to display
'orderby'=>'rand' //query random posts
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="related-post-title">Related Post:</div>
<div class="related-post-link">
<a rel="external" href="<? the_permalink()?>">
<?php the_title(); ?>
</a>
</div>
<?php
}
wp_reset_query();
}else{ //if no category is selected
echo("");
}
}
add_action('hook_top_recent_posts', 'posts');
function post_tags(){
$tagValue = get_field('recent_posts_tag');
if($tagValue){ //check if a tag is selected in the post
$args=array(
'tag_id' => $tagValue,
'posts_per_page'=>1, // Number of related posts to display
'orderby'=>'rand' //query random posts
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="related-post-title">Related Post:</div>
<div class="related-post-link">
<a href="<? the_permalink()?>">
<?php the_title(); ?>
</a>
</div>
<?php
}
wp_reset_query();
}else{ //if no tag is selected
echo("");
}
}
add_action('hook_top_recent_posts_tag', 'post_tags');
function business_posts(){
$businesscategoryValue = get_field('recent_posts_business');
if($businesscategoryValue){ //check if a category is selected in the post
$args=array(
'cat' => $businesscategoryValue,
'posts_per_page'=>1, // Number of related posts to display
'orderby'=>'rand' //query random posts
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="related-post-title">Related Post:</div>
<div class="related-post-link">
<a rel="external" href="<? the_permalink()?>">
<?php the_title(); ?>
</a>
</div>
<?php
}
wp_reset_query();
}else{ //if no category is selected
echo("");
}
}
add_action('hook_top_recent_posts_business', 'business_posts');
function business_post_tags(){
$businesstagValue = get_field('recent_posts_business_tag');
if($businesstagValue){ //check if a tag is selected in the post
$args=array(
'tag_id' => $businesstagValue,
'posts_per_page'=>1, // Number of related posts to display
'orderby'=>'rand' //query random posts
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="related-post-title">Related Post:</div>
<div class="related-post-link">
<a href="<? the_permalink()?>">
<?php the_title(); ?>
</a>
</div>
<?php
}
wp_reset_query();
}else{ //if no tag is selected
echo("");
}
}
add_action('hook_top_recent_posts_business_tag', 'business_post_tags');
The fist 2 functions are working fine, it’s the second two that are not appearing on the site. Here is a page on the site using one of the blog categories….making it appear in the sidebar:
Here is a page that should be showing a recent posts business category in the sidebar, but nothing is appearing: http://rpv.driostudio.com/for-biopharma-and-market-researchers/
This link didn’t show up: “The fist 2 functions are working fine, it’s the second two that are not appearing on the site. Here is a page on the site using one of the blog categories….making it appear in the sidebar:”
http://rpv.driostudio.com/about-us/
Hi @rmcfadd2
Your code is not compatible with a custom taxonomy. The line in question is:
'cat' => $businesscategoryValue,
Please read over the WP_Query args documentation on the WP site and read up on how you can query a custom taxonomy.
Thanks
E
The topic ‘Custom Post type taxonomies’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.