• I have a category “year” and child-category names with year numbers 1966, 1972, 1980 etc.

    I have a category “publisher” and child-categories with publisher names.

    Each post is assigned a year child-category and a publisher child-category.

    When listing posts with child-category of a specific publisher, I want to order them according to the name of the child category of category “year”.

    I looked around and didn’t find a solution.

    Thanks

    • This topic was modified 2 years, 11 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 3 replies - 1 through 3 (of 3 total)
  • The really hard way is to create a custom SQL query.

    The easier way would be to get all the terms for year and then in a loop do multiple wp queries.

    Or you could just query all the data you need, including the year terms into an array and do a php usort()

    Thread Starter hananc

    (@hananc)

    Thanks.

    Although really hard, your first suggestion is more appealing to me.

    I assume that if I construct the correct query, I will be able to use native WP functions to display the results. Am I correct?

    Yes and no.

    There are different way of building a custom SQL query.

    1. The simpler – just a Query – which will return an array ( or object ) which then you apply php functions to, but as it wont modify $posts then ‘standard’ wp functions wont work

    2. or harder, modifying the WP_Query method SQL with filters which then will return a $posts object which you then can use wp functions like the_title()

    3. or a different approach is the write a custom query and then build a $posts object

    It all depends on the research / rabbit hole you want to go down.

    If you want to do it totally the WP way then option 2 using filters such as ‘posts_where’ and ‘posts_orderby’

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sort posts by child category name’ is closed to new replies.