WordPress.org

Support

Support » How-To and Troubleshooting » Custom Query: Related Posts by Shared Tag Amount

Custom Query: Related Posts by Shared Tag Amount

  • Leo Blanchette

    @leoclipartillustrationcom

    Lets say on average your custom post type has at least 25 tags, and usually up to 50.

    Its not enough to find “related” posts by simply whether or not they share the same tags, but instead you’d want to rank them according to how many tags they share.

    I can’t seem to do this by common WP_query() methods…I think I might need a whole custom query.

    Unfortuantely I’m not familiar enough with WP’s tables to write a query that sophisticated.

    Anyone have any ideas how to do this?

Viewing 15 replies - 1 through 15 (of 20 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Try the following. Put this in your theme’s functions.php: http://pastebin.com/NnDzdSLd

    Now you can use the function “get_related_tag_posts_ids()” in your template files to retrieve the related post ids based on shared tags. The function runs many queries (depending on the amount of tags for the post and posts that have a related tag assigned to it), so it’s recommended to use it only in single.php, especially because your posts can have 50 tags.
    Example of how to use it in your theme template files (inside the loop):

    <?php
    // get 5 related posts
    $related = get_related_tag_posts_ids( $post->ID, 5 );
    if ( $related ) {
    	$args = array(
    		'post__in'      => $related,
    		'orderby'       => 'post__in',
    		'no_found_rows' => true, // no need for pagination
    	);
    	$related_posts = get_posts( $args );
    	if ( $related_posts ) {
    		echo '<h3>Related Posts</h3>';
    		echo '<ul>';
    		foreach ( $related_posts as $related_post ) {
    			echo '<li><a href="' . get_permalink( $related_post ->ID ) . '">' . $related_post->post_title . '</a></li>';
    		}
    		echo '</ul>';
    	}
    }
    ?>

    A custom sql query would be better than this solution tough.

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    Leo Blanchette

    @leoclipartillustrationcom

    My only problem now is that I don’t have any text control options in this forum to make a really really big “THANK YOU”.

    Totally cool. Vote this up, sticky it, benefit many future generations of WP coders.

    Leo Blanchette

    @leoclipartillustrationcom

    So you advise using this in “single.php” to keep resource use down. “single-image.php” where my custom post type — would presumably work the same?

    Moderator keesiemeijer

    @keesiemeijer

    Yes, every template where related posts for a single post is displayed is ok. Let me explain what the function does.

    1) get all the tags for a post (query)
    2) get all posts that have any of these tags (query) (can be 1 posts or 1000 posts)(this can be limited)
    3) loop through these posts and get all tags assigned (multiple queries for tags)
    4) loop throug tags and check if the tag is in the tags for the post (no queries).

    As you can see there will be a lot of queries to the database if you want to show related posts for multiple posts.

    I, therefore think this is not a very clean solution to your problem. I’ve tried to optimize the queries to be as light as possible but maybe it’s also possible with a single custom sql query.

    Another way to remedy this problem would be to update the database with the related posts (for all the posts) anytime you publish or update a post.

    Leo Blanchette

    @leoclipartillustrationcom

    Yes, the last thing you say is what I intended to do. My other theme (where I made my own database tables) had a rather big query that performed this operation all in one hit.

    I’m simply going to let the user decide when to update their related images (posts) – or perhaps run a chron job…each post will have a meta value having the list of related images :D.

    Just having a decent function is enough though. There will often be thousands of posts (images) to loop through, so its best to pre-run it.

    Great job on the function. I’m sure its going to help lots of people.

    Moderator keesiemeijer

    @keesiemeijer

    Moving it to the backend and/or running a cron job are good ideas. To even optimize the queries further you could use 'cache_results' => false for the get_post() function:
    http://thomasgriffinmedia.com/blog/2012/10/optimize-wordpress-queries/

    Maybe looking at the transient api is also an idea:
    http://codex.wordpress.org/Transients_API

    I’m glad you have some sort of a solution.

    Leo Blanchette

    @leoclipartillustrationcom

    This code you gave me was probably the best thing I’ve ever gotten off of this forum – I had to make some slight modifications to the custom post type I was using, but here it is in action on a few user sites:

    http://thpstockphotos.com/image/waterfall-valley-3/

    http://www.clipartillustrations.com/image/robot-rocketeer-flying-up/

    http://stereoshutter.com/image/coins/

    As you can see it works flawlessly. I credited you 😀

    Moderator keesiemeijer

    @keesiemeijer

    I finally got around making that custom sql query. I’ve made it into a plugin: http://wordpress.org/extend/plugins/related-posts-by-taxonomy/
    See the documentation on how to use the function that queries for the related posts. This only adds a few database queries per post (no matter how many tags), so now you can use it on other templates as well.

    Leo Blanchette

    @leoclipartillustrationcom

    Thats really good – very valuable. At this time there are about 30 sites that will benefit from that! Much appreciated.

    Leo Blanchette

    @leoclipartillustrationcom

    My theme shows image results and other specialized stuff, so I’m going to integrate your plugin as part of the theme. But I’m going to give credit to you in the admin area where its used and the credits area.

    Leo Blanchette

    @leoclipartillustrationcom

    Correction – it looks like you give directions on using it! There is nothing to modify. Great job.

    Leo Blanchette

    @leoclipartillustrationcom

    Not to be annoying, but I just have to say this is a real masterpiece, especially for theme integration. I literally don’t have to do much at all except write some custom output through a template.
    A+++

    I’m going to have this plugin packed into the theme and deploy/activate itself at install.
    Great job.

    Leo Blanchette

    @leoclipartillustrationcom

    So far so good. I noticed it does not look for my custom taxonomies? Also it does not show up on my cpt page. But plugging away at it and should eventually get it working.

    Moderator keesiemeijer

    @keesiemeijer

    I noticed it does not look for my custom taxonomies?

    What are you using, the widget, shortcode or function? Did you register your custom taxonomy to be public? Do the custom taxonomies show in the widget dropdown?
    http://codex.wordpress.org/Function_Reference/register_taxonomy.

    Moderator keesiemeijer

    @keesiemeijer

    Maybe it’s because you include the plugin in your theme and activate it before your theme has set up the custom taxonomy and post type.
    See how you can include things after a theme is set up:
    http://alexking.org/blog/2012/07/09/include-plugin-in-wordpress-theme

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Custom Query: Related Posts by Shared Tag Amount’ is closed to new replies.