I have to display some worpress posts and their category in external php aplication. Each post can has several categories but on the list is displayed one. I doscover thatin most cases it's enough to order by term_order in wp_term_relationships table and tahe first result. Unfortunaetly in my app there are some single posts with another category displayed than wordpress. How can I found how wordpesss select main category for posts?
2 Answers
This depends on the functions that your theme is using to get the post categories, best way I could think of getting this to work:
Go to the template's files and search for function that gets the categories, now usually it is get_category so I'll fallow a scenario with this function.
If you go to the linked codex page you'll see that this function is using get_term (some filters can be added here, if your theme is using any, you'll have to use also!) now the code for get_term is in wp-include/taxonomy.php.
So at this point you have the code that brings the category in wordpress, you can just replicate and adapt this function's code in your external app.
This is one way but it seems preatty complex.
So I'm thinking that it will be easier to create a child theme, bring the themes page that displays the category there and create a sort in the returned category array(you can set this as returned value in get_category) and use category id max or min -> and use the same one in your app.