I would like someone to help me with a snippet of code that displays a defined number of related posts within the displayed posts taxonomy.
As an example, here is what I am working on.
http://www.nolinlakerealestate.com/mls/102-scenic-dr-mammoth-cave-ky-42259-mls-1310786
[[LINK href="http://www.nolinlakerealestate.com/mls/102-scenic-dr-mammoth-cave-ky-42259-mls-1310786"]][[/LINK]]
As you can see at the bottom I am listing related properties. There is a snippet of code that is showing a random selection of posts within the custom post type of "mls".
I would also like to show a list of subdivisions ( a taxonomy ) that are in the same city taxonomy as the current post.
Logic is something like this.
Find out what city taxonomy the current post is in.
Query database for other posts in same city taxonomy.
Put together a list of subdivision taxonomies that exist on those posts.
Output list with links to the subdivision taxonomy pages, and the subdivision names.
Eg : http://www.nolinlakerealestate.com/subdivision/the-point-at-nolin
Solution - 1
You need to use WP_Query. Something like this:
global $post;
// this gets the term(s) for the subdivison tax for the current post
if( $terms = wp_get_object_terms($post->ID, 'subdivision') ) {
// Use the first one (you could use more) to get other posts:
$args = array(
'post__not_in' => $post->ID, // we don't want the current post!!
'tax_query' => array(
array(
'taxonomy' => 'subdivision',
'field' => 'slug',
'terms' => $terms[0]->slug
)
)
);
$query = new WP_Query( $args );
while($query->have_posts()):
$the_query->the_post();
// Do whatever layout you want.
echo ' ';
echo the_title();
echo '';
endwhile;
}
Solution - 2
there might be a better way to do this, but the easiest way that comes to mind is to do a custom sql query. First, you need to get the City of the current post: $cities = get_the_terms(get_the_ID(),'city');
//and we'll get the subdivision so we can exclude it later:
$subdivisions = get_the_terms(get_the_ID(),'subdivision');
This will give you an array (obviously each post will only have one city, but wordpress doesn't have a function to return only one taxonomy item) so we need to get down to a single item:
if ( $cities && ! is_wp_error( $cities ) ) :
$city_id='';
foreach ( $cities as $city ) {
$city_id = $city->term_id;
}
if ( $cities && ! is_wp_error( $cities ) ) :
$subdivision_id='';
foreach ( $subdivs as $subdiv ) {
$current_subdiv_id = $subdiv->term_id;
}
endif;
Now, we need to find all (or some) of the subdivisions with posts within this state:
global $wpdb;
$subdivs=$wpdb->get_col(
$wpdb->prepare(
"select distinct(t.term_id) from $wpdb->terms t
join $wpdb->term_taxonomy tt on tt.term_id = t.term_id
join $wpdb->term_relationships tr on tt.term_taxonomy_id = tr.term_taxonomy_id
join
(select id from $wpdb->posts p
join $wpdb->term_relationships tr on p.id = object_id
join $wpdb->term_taxonomy tt on tt.term_taxonomy_id = tr.term_taxonomy_id
where tt.term_id = $city) p on p.id=tr.object_id
where tt.taxonomy = 'subdivision'
and t.term_id<>$current_subdiv_id
limit 5"
));
$subdiv_links = Array();
foreach($subdivs as $subdiv_id){
$subdiv=get_term_by('id',$subdiv_id,'subdivision');
$subdiv_links[]=''.$subdiv->name.'';
}
Solution - 3
try this
define this in functions.php
function tax_related_posts($taxonomy = '') {
global $post;
if($taxonomy == '') { $taxonomy = 'post_tag'; }
$tags = wp_get_post_terms($post->ID, $taxonomy);
if ($tags) {
$first_tag = $tags[0]->term_id;
$second_tag = $tags[1]->term_id;
$third_tag = $tags[2]->term_id;
$args = array(
'post_type' => get_post_type($post->ID),
'posts_per_page' => 4,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => $taxonomy,
'terms' => $second_tag,
'field' => 'id',
'operator' => 'IN',
),
array(
'taxonomy' => $taxonomy,
'terms' => $first_tag,
'field' => 'id',
'operator' => 'IN',
),
array(
'taxonomy' => $taxonomy,
'terms' => $third_tag,
'field' => 'id',
'operator' => 'IN',
)
)
);
$related = get_posts($args);
$i = 0;
if( $related ) {
global $post;
$temp_post = $post;
foreach($related as $post) : setup_postdata($post);
$content .= ' ';
endforeach;
$post = $temp_post;
}
}
return $content;
}
and call it as
or
or
see these two question
http://wpquestions.com/question/show/id/1603
http://wpquestions.com/question/show/id/2659
|