I’m currently trying to get into WordPress to solve a problem for a non-profit project I’m working with. Basically I wanted to have a Notion like databases system for people (with cities and interests) and projects (with cities and interests), with people also associated to projects.
So I was suggested to use Custom Post Types, Taxonomies and Custom Queries. I’m using the basic Advanced Custom Fields.
I created the 2 post types for Projects and People and then taxonomies for cities and interests and filled with a few examples.
I know wanted to be able to lists both People and Projects (a page for each) to eventually filter and search by the taxonomies. Also, there don’t seem to be backlinks between the terms or projects and the posts.
I’m not being able to reach a post’s taxonomy terms though, what am I missing? I’ve done multiple iterations of this.
// The Loop
if ( $query->have_posts() ) {
?>
<ul>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php $terms = wp_get_object_terms( $query->post->ID, 'interest'); ?>
<p><?php echo is_array($terms) ? count($terms) : ''; ?></p>
<?php foreach ( $terms as $term ) : ?>
<p><?php echo $term; ?>
<?php endforeach; ?>
</li>
<?php
# This should be working, unless the posts aren't truly linked to the taxonomies
# https://wordpress.stackexchange.com/questions/152510/getting-custom-taxonomy-from-custom-post-type
# /questions/50930583/displaying-custom-post-types-taxonomy
#$term_obj_list = get_the_terms( $query->post, 'interest' );
#$terms_string = join(', ', wp_list_pluck($term_obj_list, 'name'));
#var_dump($terms_string);
?>
<?php endwhile; ?>
</ul>
<?php
But the following works:
echo "<hr> <h4> All the available options </h4>";
# /questions/53422729/list-all-taxonomies-assigned-from-a-custom-post-type
$taxonomies = get_object_taxonomies( 'person', 'objects' );
foreach( $taxonomies as $taxonomy ){
echo "<h5>{$taxonomy->label}</h5>";
$terms = get_terms(array(
'taxonomy' => $taxonomy->name,
'hide_empty' => false,
));
foreach( $terms as $term ){
$link= get_term_link($term);
$label=$term->name;
echo " <a href='{$link}'>{$label}</a>";
}
}
I’m guessing the posts are not associated to the terms properly?
Because I can list the terms associated to the taxonomies associated with a post, but not get the terms for a specific post.