Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Taxonomy / related posts WordPress

  • SOLVED

Hi there - i'm currently running a music website (http://www.wearepopslags.com), using categories (videos, music, photos, interviews) and tags for artists' names. That's working fine, however, I would like to show related items in the sidebar. I.e. on an article for Madonna, I would like to show:
- 5 'image' posts by Madonna
- 5 'video' posts by Madonna
- 5 'interview' posts by Madonna
- 5 'music' posts by Madonna

I have a feeling using tags isn't right, as a lot of posts will have been tagged with multiple artists (i.e. a post about Madonna's album Hard Candy, would also be tagged by Timbaland the producer, and obviously I wouldn't want to show articles solely about him in the side view).

I'm essentially after something like Contact Music (sidebar and the single up for a newsletter, again based on a tag) - where they only show related content for Rihanna, eventhough the article has been tagged with various artists (note CM isn't build on Wordpress).

http://www.contactmusic.com/news.nsf/story/rihanna-tweets-britney-after-remix-release_1212037

Any idea how I might be able to get the same results on Wordpress? Should I be using custom taxonomy and if so, how should I set this up?

Thanks
Silvan

Answers (2)

2011-07-17

Gabriel Merovingi answers:

Hey!

This functionality, are you looking for it to be a sidebar widget or a function that you can call anywhere in your WordPress theme?

You say you have tags for artists names, its only names in there and nothing else?

// Gabe


silvanovicz comments:

Hi Gabe - no need for a widget, just a function. I think it's to actualy use the new taxonomy functionality in wordpress (adding in some code in the template/functions.php) and then add some code to the sidebar to bring up the articles in those sections.

I'm just not quite sure where to start...

Cheers
Silvan


Gabriel Merovingi comments:

Ok I got you this function.
It searches for posts that have the "artists" (tag) in a specific category. In the example bellow I have asked for 5 posts from the category "Music" from the artist "Madonna". Then I ask for 5 posts from the category "Interviews" with the same artist. You can use this function as often as you want just save it in your functions.php.

You still need to adjust what you want to show, link and title etc. Also you should insert the current posts id (when on single page) to either hide it or highlight it amongst results. This requires WP 3.0 +


function get_results_by_artist( $current_post = 1, $artist, $type, $title, $number_of_posts )
{
$args = array(
'category_name' => $type,
'tag' => $artist,
'posts_per_page' => $number_of_posts
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
echo '<h1>' . $title . '</h1>';
while ( $query->have_posts() ) : $query->the_post();
// Results
echo '<p';
if ( get_the_ID() == $current_post ) echo ' style="color:red;"';
echo '>' . get_the_title() . '</p>';
endwhile;
else :
// optional can be removed
echo '<p>No results</p>';
endif;
wp_reset_postdata();
}
// Example 1:
get_results_by_artist( 39, 'Madonna', 'Music', 'Music with Madonna', 5 );
// Example 2:
get_results_by_artist( 2, 'Madonna', 'Interviews', 'Interviews with Madonna', 5 );


silvanovicz comments:

Hi Gabe - thanks for the code above. I'm getting results back (http://dev.wearepopslags.com), but it's not quite there yet. I pasted the code in sidebar.php. Is that right, or should it go into functions?

- the code currently manually sets the tag as 'madonna', how can this be set dynamically? Keep in mind that some articles have multple tags.
- in your code you use 'get_results_by_artist( 39,...' - what does the '39' and '2' refer to?
- how can you make those articles link (they're just text at the moment and I would like them to link).

Thanks
Silvan


Gabriel Merovingi comments:

You say you have multiple artists, do you want to show the results for all of them or just a specific one that you set?

The function goes into the functions.php. While the examples goes in your sidebar.
The number in the call is for the current post id. When on a single page you can pass the_ID() giving the function the current posts ID.

The way the function is written above if the post ID is the same as in the results it highlights it by changing its color but if you want it can also be used to make sure no duplicates are shown. What would be your preference?

If you want you can save a custom field with an artists name that we filter by for each post.
in that case you would call it :


$current_posts_id = get_the_ID();
$artist_to_look_for = get_post_meta( $current_posts_id, 'artist_to_use', true);
get_results_by_artist( $current_posts_id, $artist_to_look_for, 'Music', 'Music with ' . $artist_to_look_for, 5 );

get_results_by_artist( $current_posts_id, $artist_to_look_for, 'Interviews', 'Interviews with ' . $artist_to_look_for, 5 );



If you want to instead of highlighting current posts, exclude them then the function should be:


function get_results_by_artist( $current_post = 1, $artist, $type, $title, $number_of_posts )
{
$args = array(
'category_name' => $type,
'tag' => $artist,
'posts_per_page' => $number_of_posts
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
echo '<p>' . $title . '</p>';
while ( $query->have_posts() ) : $query->the_post();
// Exclude the current post
if ( get_the_ID() != $current_post ) {
echo '<li><a href="'.get_permalink() .'" title="' . get_the_title() . '">' . get_the_title() . '</a></li>';
}
endwhile;
else :
// optional can be removed
echo '<p>No results</p>';
endif;
wp_reset_postdata();
}


Note that I changed the layout of results from just a title to a title with permalink but of course you can change this to anything.


Gabriel Merovingi comments:

If you don't want to use Custom Fields we can insert a box into your edit post window. Just copy the code bellow into your functions.php:


// Create a meta box where we can select our artist
add_action( 'add_meta_boxes', 'the_artist_box' );
function the_artist_box() { add_meta_box( 'artist-to-display', 'Artist', 'our_artist_selection', 'post', 'side', 'high' ); }
function our_artist_selection()
{
global $post;
// See if have saved any
$artist_selected = get_post_meta( $post->ID, 'artist_to_show', true );
$arguments = array(
'taxonomy' => 'post_tag',
'show_option_none' => 'None',
'selected' => $artist_selected
);
echo '<p>Select Which artist to show:</p>';
// We create a dropdown menu of ALL post tags that are connected to a post.
wp_dropdown_categories( $arguments );
}
// Save our details.
add_action( 'save_post', 'save_artist_selection' );
function save_artist_selection( $post_id )
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
update_post_meta( $post_id, 'artist_to_show', $_POST['cat'] );
}
// Get Results by artists (goes into functions.php
function get_results_by_artist( $current_post = 1, $artist, $type, $title, $number_of_posts )
{
$args = array(
'category_name' => $type,
'tag' => $artist,
'posts_per_page' => $number_of_posts
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
echo '<p>' . $title . '</p>';
while ( $query->have_posts() ) : $query->the_post();
// Exclude the current post
if ( get_the_ID() != $current_post ) {
echo '<li><a href="'.get_permalink() .'" title="' . get_the_title() . '">' . get_the_title() . '</a></li>';
}
endwhile;
else :
// optional can be removed
echo '<p>No results</p>';
endif;
wp_reset_postdata();
}

This code will create a custom meta box that you can view when editing a post. It will be placed on the top right hand corner of your screen (if you using 2 column view). It generates a list of all your tags and you can select one of them to use.

In your single.php file you call the function as before:


// How to call the function anywhere on your single page file.
$current_posts_id = get_the_ID();
$artist_to_look_for = get_post_meta( $current_posts_id, 'artist_to_show', true );
// Call the function for each item you want to show i.e. 5 x Music and 5 x Interviews
get_results_by_artist( $current_posts_id, $artist_to_look_for, 'Music', 'Music with ' . $artist_to_look_for, 5 );
get_results_by_artist( $current_posts_id, $artist_to_look_for, 'Interviews', 'Interviews with ' . $artist_to_look_for, 5 );


silvanovicz comments:

Hi Gabe - it's getting there, but I'm not quite getting the right results. I pasted the code snippet in single.php (underneath the FB comments box) and when I don't select an artist, I get random results in the correct categories. But when I select an article, it returns 'no results'

Beyonce tagged (no results):
http://dev.wearepopslags.com/beyonce-waiting-new-song/
http://dev.wearepopslags.com/alicia-keys-and-beyonce-video/
http://dev.wearepopslags.com/alicia-keys-or-lady-gaga-ft-beyonce/

Non tagged (random results by correct categories):
http://dev.wearepopslags.com/listen-3-kylie-demos-from-the-x-sessions/
http://dev.wearepopslags.com/tori-amos-to-perform-in-london/

Ideally I'd like the results to appear in the sidebar, rather than in the single.php area.

S

PS - is there any way that list can be order alphabetically; i have about 300 tags and at the moment they seem to be order randomly.

2011-07-15

Romel Apuya answers:

I can do it for you.


silvanovicz comments:

how would you go about doing this? Would you make the changes in the code / theme?


Romel Apuya comments:

yes. that would be the best thing.


Romel Apuya comments:

try also

http://wordpress.org/extend/plugins/yet-another-related-posts-plugin/


silvanovicz comments:

i'm already running YARP, but that's just bringing up related items. I want to split them out by type (video/photos/news/etc) and artist.