• Hello,
    Im trying to create a “lreated content” sidebar, but with more relevent articles pushed to the top.

    The articles are a Custom Post Type, with Custom Taxonomies, and im trying to make no extra work/tweaks on the admin side either.

    The current code im trying is here:
    https://pastebin.com/cF1XcshH

    Ideally I would like to return posts based on:
    Category 1 AND Category 2
    – Run Loop

    Category 1 OR Category 2
    – Run Loop (but exclude posts returned in first loop)

    Is it possible to do this, with one single query/loop?
    Or would it have to be 2 seperate loops?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Ideally, there would be a “related strength” score with which to order posts. But there is no such score and I’m unsure how it would accurately be determined anyway. What you want to do instead is list all posts which have any of the current post’s category terms, with posts assigned the most terms in common listed first. Am I understanding correctly? Your example assumes there are two terms, but I’ve rephrased my interpretation to account for any number of terms. Is that reasonable or will there always only be two terms?

    Either way, I think the solution is to query for all posts assigned any of the terms (OR logic). Then sort the posts by how many matching terms there are. There may be a way to sort this way in SQL, but it’s beyond my abilities. What I would do is use PHP to sort the returned array of posts with usort(). Start by adding another array dimension which contains the count of matching terms so this does not need to be repeatedly determined by the sort algorithm. Then create the usort callback that returns the difference in counts for every passed pair. Output the top n number of posts after the sort.

    YARPP (Yet Another Related Posts Plugin) should be able to do this out of the box for you. You may want to give it a try. Let me know if you have any questions re YARPP.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Related content + Loops’ is closed to new replies.