Could someone point me in the right direction here. Basically I want to query all the tags under one category and then List each tag with their related posts underneath. I would like the posts to link to the article.
Thanks
Could someone point me in the right direction here. Basically I want to query all the tags under one category and then List each tag with their related posts underneath. I would like the posts to link to the article.
Thanks
I am a little confused. Do you want to list links to posts that belong to a certain category, grouped under tags?
In other words, for each tag, find all posts having that tag and in a certain category, and list the links to them?
Hey vtxyzzy, Yup, thats exactly what Im after. I know it sounds funny but if I told you whole idea it would make sense. Is this possible?
Sure it is, but it will take a little time to code up an example. I'll work on it a bit and post back.
Thanks for your help, I was trying to piece the codes I found together but not knowing much about coding I of course failed. Here is what I was using.
<?php
$args = array('categories' => '246');
$tags = get_category_tags($args);
$content .= "<ul>";
foreach ($tags as $tag) {
$content .= "<li><a href=\"$tag->tag_link\">$tag->tag_name</a></li>";
}
$content .= "</ul>";
echo $content;
?>
This basically gets the tags for the category but not the posts. I was trying to insert a get_post query but wasn't sure how. Thanks again
I think this is what you want. I took out the list code because I am not sure it will work when you are displaying post content between list items. Also, you probably want to put in some divs for styling.
<?php
$args = array('categories' => '246');
$tags = get_category_tags($args);
foreach ($tags as $tag) {
$my_query = new WP_Query("caller_get_posts=1&tag=$tag->tag_name");
if ($my_query->have_posts()) :
echo "<h2><a href=\"$tag->tag_link\">$tag->tag_name</a></h2>";
while ($my_query->have_posts()) :
$my_query->the_post();
// put code here to display what you want from each post
endwhile;
endif;
}
?>Thanks, I inserted it into my template and nothing shows up at all. What am I missing?
Hmm, I messed around with changing the category and found for some reason it don't list all the tags under a category and that is why the tags for this particular category are not showing up. Its only listing some of them. The particular category I'm interested in is new and at this point only has a few posts and a few tags, none of the tags show. I'm still trying to figure out why only some are showing up.
OK, observation, all the tags it returns are one syllable, that's the issue I think. How do I fix that?
OK, the code I posted has a couple of problems.
First the tag parameter needs to use the tag slug which is not available in the version of get_category_tags that I have. Instead, use tag__in, like this:
$args = array(
'caller_get_posts' => 1,
'tag__in' => array($tag->tag_id),
);
$my_query = new WP_Query($args);
Unfortunately, that retrieves all posts with that tag, not just in the category. That is a little harder to fix. First, add this to your functions.php:
<?php // Define the join filter function
function mam_posts_join ($join) {
global $mam_global_join;
if ($mam_global_join) $join .= " $mam_global_join";
return $join;
}
// Add the filter to the hooks
add_filter('posts_join','mam_posts_join');
?>
Then use this for your template:
$categories = '246';
$mam_global_join = "JOIN $wpdb->term_relationships tr
ON (tr.object_id = $wpdb->posts.ID)
JOIN $wpdb->term_taxonomy tt
ON (tt.term_taxonomy_id = tr.term_taxonomy_id
AND tt.taxonomy = 'category'
AND tt.term_id in ($categories))";
$tagargs = array('categories' => $categories);
$tags = get_category_tags($tagargs);
foreach ($tags as $tag) {
$args = array (
'caller_get_posts' => 1,
'tag__in' => array($tag->tag_id),
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
echo "<h2><a href=\"$tag->tag_link\">$tag->tag_name</a></h2>";
while ($my_query->have_posts()) :
$my_query->the_post();
// put code here to display what you want from each post
endwhile;
endif;
}
?>
I tested this in a template in the WP 2.9 default theme.
Awesome, thanks for your help and it is now working great!
Glad its working! Now, please use the dropdown at top right to mark this topic 'Resolved'.
This topic has been closed to new replies.