0

I am having trouble debugging a situation, and I think some better background information on how these systems work would be very helpful. I know that the use of the function query_posts is strongly discouraged. But let's just assume that there is nothing I can do to remove it from the code. Specifically the query is changed to pull posts of a post_type (a post_type specific to the theme I am using). This is what the query looks like after it has been changed:

posts_per_page=10&paged=0&post_type=project

Then the loop begins and it can successfully grab the titles for each post. But when I call get_the_category it returns an empty array, even though all of the posts have categories. I verified that it was an empty array with var_dump.

I do not have a strong understanding of how these systems work, so the emphasis on not using query_posts has me worried. Is there any possible interaction between query_posts and get_the_category that could cause it to not work correctly?

3
  • Since this is a custom post type, does it have a custom taxonomy or is it actually the category tax? And would you mind sharing why exactly you can not do what you want by using WP_Query?
    – tfrommen
    Feb 26 '14 at 22:32
  • Ahh, I am not sure about that, but from the admin dashboard the categories are listed under the same section as the project posts, they aren't listed under the normal posts. Does that mean these are custom categories? Feb 26 '14 at 22:36
  • Well, if there is a new submenu entry for these categories then it's most likely a custom taxonomy. The name/slug of the tax will be stated in the URL if you click to edit the categories. Then just use get_the_terms for that taxonomy.
    – tfrommen
    Feb 26 '14 at 22:40
1

The "main" query on the page is saved to a global called $wp_query. That global determines a great deal about the page, including both content and which template is used to display that content. Using $wp_query overwrites that main query and can cause some very basic things to fail but get_the_category() is probably not one of them.

It is more likely that there is something wrong with your Loop. What matters for get_the_category() is another global called $post. $post is set at each iteration of a proper Loop to the "current post". If get_the_category() is not passed a post ID, it will assume that $post is the post in question.

Without seeing the code, that is the best I've got.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.