how do i produce a list of wp posts based on taxonomy tag in my pod page?

0

here is the scenario. I have a pod page of medical treatments. This is the detail page, and on the bottom of this page I want a list of the related news posts that are tagged with the same treatment name. The news posts are regular wp posts, with tags "news" and "[treatment name]"

e.g. if my pod detail page is "heart surgery" then, at the bototm of the page I want a list of all the news articles that are tagged with "heart surgery" (and of course "news") the pod's treatment name will be the same as the taxonomy tags used in my news articles.

what would be the best way to do achieve this?

asked Jan 10 at 1:37

johnnyn

3

add comment
enter at least 15 characters

3 Answers

1

I actually have this on my list to add to one of my sites. Thanks for spurring me to do the research on it now.

It looks like you can do something like this. See query_posts() on the WP-Codex for more information. You can also take a look at get_posts(), but the documentation there is severely lacking.

query_posts('tag=news+heart-surgery');
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
 while (have_posts()) : the_post();
  the_content( 'Read the full post ยป' );
 endwhile;

You should be able to create a pick field on the related taxonomy terms from within pods. I haven't tried this yet. Note that the tag you pass to query_posts() should be the tag's slug.

If you get this working, please post your code. I'm probably a month away from implementing this myself.

answered Jan 10 at 2:48

chris.pilko

889

add comment
enter at least 15 characters
0

thanks Chris, I will post what i end up doing.

answered Jan 10 at 4:42

johnnyn

3

add comment
enter at least 15 characters
0

here is what i ended up using, thanks for the pointer Chris.

<ul>
<?php
global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 3, 'category_name' => 'news+heart-surgery' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>
</ul>

I believe the 'category name' argument uses a comma as "or" and a plus as "and" but I don't think you can mix "AND" "OR", for example, the following did not work for me.

'category_name' => 'news+heart-surgery, news+heart-disease'

but these did work

'category_name' => 'news+heart-surgery+heart-disease'
'category_name' => 'news,heart-surgery,heart-disease'

I have used categories, but tags works just as well if you want to filter by tags instead of categories. Just change the argument as below.

 'tag' => 'news+heart-surgery'

I don't see any way in the API to get both some articles from a category as well as some articles from tags, so you would probably have to run get_posts() twice and then join the results up.

update... this is what i put on my pods detail page... I have a field in my pod called "tag" this is what i am filtering by as well as the category "news"

<?php
global $post;
$tmp_post = $post;
$myNewsFilter = 'news+'.$pods->get_field('tag');
$args = array( 'numberposts' => 3, 'category_name' => $myNewsFilter );
$myposts = get_posts( $args );
if($myposts){ // check that there are any related news articles
?>
<h2>Related news articles</h2>
<ul>
<?php
foreach( $myposts as $post ) : setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php }// end if statement checking that related news exists    ?>
<?php $post = $tmp_post; ?>

you could probably use templates to do a better job

--- edit you can use categor_name as well as tag in the arguments. just add them both and it works.

answered Jan 11 at 3:21

johnnyn

3

edited Jan 13 at 11:34

add comment
enter at least 15 characters