ok, have no clue about how I did it but mixing a ‘related post by tag’ code and the one above I got what I was looking for.
Now, since I have no php programming skills maybe you guy could just tell me if the following code is ‘correct’, I mean, it seems to provide the right output (on MAMP), but it is useful to understand if it is a ‘clean’ code that a third person searching this thread can safely use for another theme.
<!-- related projects -->
<?php $orig_post = $post;
global $post;
$tags = get_the_terms( get_the_ID(), 'skills' );
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'term' => $term->slug,
'taxonomy' => $term->taxonomy,
'post_type' => 'portfolio',
'post_status' => 'publish',
'exclude' => get_the_ID(), /* exclude current project */
'post__not_in' => array($post->ID),
);
$my_query = new wp_query( $args );
/* Loop */
if( $my_query->have_posts() ) {
echo '<div id="relatedposts"><h3>Related Posts</h3><ul>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<li>
<div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div class="relatedcontent">
<h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<?php the_time('M j, Y') ?>
</div>
</li>
<? }
echo '</ul></div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
mhh.. I didn’t see that actually this code shows all the custom post without filtering it by current single post taxonomy, this is the code I am using right know, can you guys help me to understand how to exclude not related taxonomy posts?
<!-- related projects -->
<?php $orig_post = $post;
global $post;
$tags = get_the_terms( get_the_ID(), 'skills' );
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'term' => $term->slug,
'taxonomy' => $term->taxonomy,
'post_type' => 'portfolio',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
);
$my_query = new wp_query( $args );
/* Loop */
if( $my_query->have_posts() ) {
echo '<div id="relatedposts">
<h3>Related Posts</h3>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div class="post-thumb">
<a href="<? the_permalink()?>" rel="bookmark" title="">
<div class="view">View</div>
<?php
if(has_post_thumbnail()) {
the_post_thumbnail();
} else {
echo '<img src="'.get_bloginfo("template_url").'/images/default-thumbnail.png" />';
}
?>
</a>
</div>
<div class="relatedcontent">
<h4><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
</div>
<? }
echo '</div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
Try it with a tax_query: https://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters
example [untested]:
$args = array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'tax_query' => array(
array(
'taxonomy' => $individual_tag->taxonomy,
'field' => 'slug',
'terms' => array($individual_tag->slug)
)
)
);
$my_query = new WP_Query( $args );
taxonomy_exists?
if taxonomy echo thumbnail
thank you for your reply guys,
keesiemeijer, your tip worked!
so, as you said, instead of
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'term' => $term->slug,
I have to write this:
'tax_query' => array(
array(
'taxonomy' => $individual_tag->taxonomy,
'field' => 'slug',
'terms' => array($individual_tag->slug)
so the whole thing become
<?php $orig_post = $post;
global $post;
$tags = get_the_terms( get_the_ID(), 'skills' );
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args = array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'tax_query' => array(
array(
'taxonomy' => $individual_tag->taxonomy,
'field' => 'slug',
'terms' => array($individual_tag->slug)
)
)
);
$my_query = new WP_Query( $args );
/* Loop */
if( $my_query->have_posts() ) {
echo '<div id="relatedposts">
<h3>Related Posts</h3>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div class="post-thumb">
<a href="<? the_permalink()?>" rel="bookmark" title="">
<div class="view">View</div>
<?php
if(has_post_thumbnail()) {
the_post_thumbnail();
} else {
echo '<img src="'.get_bloginfo("template_url").'/images/default-thumbnail.png" />';
}
?>
</a>
</div>
<div class="relatedcontent">
<h4><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
</div>
<? }
echo '</div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
I am going to close the thread, meanwhile can you explain how this change solved the issue (I’d like to have a better PHP understandment)?
In your query ($args) you used “term” which isn’t a WP_Query parameter: https://codex.wordpress.org/Function_Reference/WP_Query#Parameters
And you got the taxonomy and term slug from the foreach: foreach($tags as $individual_tag) { ... }
, so you have to use “$individual_tag” and not “$term” as the variable to get the term’s taxonomy and slug.
I’m glad you got it resolved.
Hey guys this is just what I was looking for but when I use your code I get a: Parse error: syntax error, unexpected T_ENDWHILE
I’m working on wampp with wordpress 3.3.1
Thanks for any help.