Hello,
I’m certain this can be improved upon, but here’s my solution after tinkering with it for a few minutes:
add_shortcode('tax', 'tax_function');
function tax_function() {
$issues = get_terms('issue');
$issue_array = array();
foreach($issues as $issue) {
$issue_array[] = $issue->slug;
}
$args = array( 'numberposts' => '1',
'tax_query' => array(
array(
'taxonomy' => 'issue',
'field' => 'slug',
'terms' => $issue_array,
),
)
);
$recent_posts = wp_get_recent_posts( $args );
$term = wp_get_post_terms($recent_posts[0]["ID"], 'issue');
$the_issue = $term[0]->slug;
$loop_args = array( 'numberposts' => '10',
'tax_query' => array(
array(
'taxonomy' => 'issue',
'field' => 'slug',
'terms' => $the_issue,
),
)
);
$the_query = new WP_Query( $loop_args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
}
Obviously you’ll probably want to change it from using a shortcode to whatever implementation method you prefer, but this worked for me.
I hope this is helpful.
Cheers.
Hey Kendall,
Thanks for taking the time to write that function out. It seems like nothing is being retrieved though and I’m getting NULL on a var_dump of $the_query.
Any further advice or guidance?
Many thanks!
I really sort of meant for the above to be used as an example rather than a drop in fix. Make sure the args are correct for your site and echo the results of each variable along the way to figure out where the break is happening.
That’s just the thing I’m pretty new to WordPress development and don’t understand too well how it works. I’m dumping each variable and getting either nothing or NULL. The taxonomy term is “issue” so what you’ve written looks fine to me but I’m not really sure how to debug further.
Any clues?
Is the site on a server somewhere or do you have it on a localhost?
I’m working locally on a new theme but with a DB of an existing site.
Can you whip up a backup of the site and database right quick, throw it up on a server or in Dropbox and email me a link?
BackUpWordPress works well for this.
kendall at peppermintmedia dot net
I figured it out, wasn’t calling the function correctly. What you had actually worked perfectly. Thanks Kendall!
Glad I could help. If that’s it for this issue, would you mind marking the topic as “resolved” so that others in the future can know to reference it when they have similar issues?
Thanks a bunch.