• I know how to display posts of a specific category or sub-category, but I need to separate my posts of a category between those tagged and those not tagged.

    Example: Client wants to display a list of service providers by type of service, let’s say “Legal Services”. But they also want to tag some of them as “Recommended”. When displaying all “legal service’ providers on the page, how do I seperate those tagged as ‘recommended’ and display those before the others without that tag?

    Sub-categories is one way, but there are so many categories and sub categories of service providers from different industries that making the ‘recommended’ a sub-category of each top level category is no longer that good of an option.

    I’ve tried searching for identifying and displaying posts by tag, but haven’t found what I’m looking for.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    A custom WP_Query can get all posts in one category and which have a tag assigned or not assigned by using the “tax_query” argument structure. This would entail two queries, one for each tag state.

    Alternately, make a custom SQL query that gets all posts in a category and orders the posts found by their assigned tag terms. Posts without tags will naturally fall to the bottom of the list. WP_Query cannot order by taxonomy terms, which is why a custom SQL needs to be written.

    Your only other option would be to simply get all posts in the category and sort the results with PHP’s usort() function. The problem here is pagination doesn’t work very well. You must fetch all posts in order to get the right ordering so that the pagination can be determined. On the next page request, the entire process has to be repeated.

Viewing 1 replies (of 1 total)
  • The topic ‘Splitting out posts of category by tag’ is closed to new replies.