• Hello, I’ve been searching high and low for this. Every time I think I’ve found something I get turned around because my post is a custom post type. I’m using a theme that has a CPT of Portfilos. What I’d love to do is take a post ID and look up the first Portfolio Category that is assigned. I need to have elements in my template that are conditional depending on which Portfolio Category a post is in.

    The only way I can think of determine anything about the post is via it’s ID, or maybe slug, if I could figure out how to derive that. ??

    Any ideas?
    Thanks!

Viewing 9 replies - 16 through 24 (of 24 total)
  • Thread Starter 8900200

    (@8900200-1)

    Wait. Got it.

    portfolio-category, not categories! They had an internal name diff from the UI… of course ??

    array(2) { [0]=> object(stdClass)#66 (9) { [“term_id”]=> string(3) “114” [“name”]=> string(20) “Home – Visual Design” [“slug”]=> string(18) “home-visual-design” [“term_group”]=> string(1) “0” [“term_taxonomy_id”]=> string(3) “132” [“taxonomy”]=> string(18) “portfolio-category” [“description”]=> string(0) “” [“parent”]=> string(3) “115” [“count”]=> string(1) “2” } [1]=> object(stdClass)#65 (9) { [“term_id”]=> string(1) “3” [“name”]=> string(13) “Visual Design” [“slug”]=> string(13) “visual-design” [“term_group”]=> string(1) “0” [“term_taxonomy_id”]=> string(3) “110” [“taxonomy”]=> string(18) “portfolio-category” [“description”]=> string(0) “” [“parent”]=> string(3) “105” [“count”]=> string(1) “6” } }

    Thread Starter 8900200

    (@8900200-1)

    Ah, now I’ve hit the dreaded multiple category assignment issue. Wonder how I can a list of all the categories a post belongs too….

    What is a “dreaded multiple category assignment” issue? I’ve never once dreaded multiple categories.

    Thread Starter 8900200

    (@8900200-1)

    Well, if some posts are assigned to 3 categories and some are only in 1 then functions such as wp_get_object_terms( I believe) only return the first category they find. I’ve seen a few caveats next to functions like this and plugins that say that they don’t play well when posts are assigned to multiple categories.

    In this case, I had a problem with using the first category provided by the terms function since I wanted to use the ID to look for all other posts in the category. The problem happens when I get to a sibling posts that also have another cat assigned, and that one comes up first from these functions. As soon as the user gets to one of those posts the prev-next code grabs its ID and start creating prev-next links outside of the original cat that they landed on. Clear as mud? ?? I could look for ways to hard code things, but I’d rather have it done pragmatically.

    Well, if some posts are assigned to 3 categories and some are only in 1 then functions such as wp_get_object_terms( I believe) only return the first category they find.

    Where did you get that information? The code you posted 4 comments up shows 2 categories– term_id=114 and term_id=3. You can tell by the first few characters array(2). No offense but you seem to be assuming a lot and researching maybe not so much.

    Also, you are pulling taxonomies by a post ID. It doesn’t matter if some are something and others are something else. You are only dealing with one at a time.

    I really don’t know what your last paragraph means.

    Thread Starter 8900200

    (@8900200-1)

    No offense taken. I’ve been researching a lot though. Just take a look at this for example…https://www.studiograsshopper.ch/code-snippets/category-description-with-multiple-categories-and-get_the_category/

    Also, you are pulling taxonomies by a post ID. It doesn’t matter if some are something and others are something else. You are only dealing with one at a time.

    You are assuming a lot too. This comment indicates that. If you read it and understood that the use of the IDs is to reference other posts by creating previous and next links to them you’d “get” that there is a problem.

    If you read it and understood that the use of the IDs is to reference other posts by creating previous and next links to them you’d “get” that there is a problem.

    You are talking about something significantly different from everything else in this thread and quite different from your initial description of the problem–“What I’d love to do is take a post ID and look up the first Portfolio Category that is assigned.” We did that. And look at every example in this thread. All of them involve pulling your custom taxonomy by means of a post ID.

    And I did say that “I really don’t know what your last paragraph means.”

    ‘next’ and ‘previous’ doesn’t make any sense without some context. Your posts don’t really have an order. It all depends on how you pull them from the database. So by ‘next’ and ‘previous’ I assume you mean something like “chronologically next door– either younger or older– to the post in question”? I’m not sure there is anything built in to do that except when you are already in the context of a category archive, and it sounds like you aren’t. I don’t have a quick solution to this one but I think you may end up running a query for each of your categories, which could be a significant load on the server.

    Thread Starter 8900200

    (@8900200-1)

    Hey. It’s all good. I do appreciate your time and help.

    I tried to mention that I was using a plugin that literally created a loop through a set of posts based on category… or in this case custom taxonomies. It’s also in my code. I get that I may be looking at this in a different way than many people do, but as is par for the course with wordpress there are times when the status quo isn’t helping the developer. This is one of them. Just note the many posts on the net related to dealing with these issues. And your concerns about server load times. The bottom line for me is how hard it is to determine a primary category, in a sense, and them what to do with the other ones. In the end I made sure they all only had one and now I’m good.

    Sure, you could say that I need to get closer to the functions and understand how they are built a little better. I did look at those functions closely. But my point is that the func reference doesn’t say anything about how it deals with multiple results (that I saw). BTW – that array(2) that you mentioned. I had two calls going one after the other the same dump but with different categories being called at one point and frankly didn’t see the second dimension.

    Best
    Rich

    but as is par for the course with wordpress there are times when the status quo isn’t helping the developer. This is one of them.

    I do understand that. I find some things unreasonably difficult, but I also understand that the developers can neither predict nor accommodate everything. Plus you can always write a patch and submit it if you want something changed. It might get picked up.

    The bottom line for me is how hard it is to determine a primary category…

    It is hard because there isn’t one. Categories just aren’t organized that way. Of course, you can ‘crawl’ up to the top level of a category tree but two top-level categories are essentially equal. There is no primary/secondary distinction. Maybe that is a flaw or an oversight, or maybe it just isn’t much in demand. It shouldn’t be terribly hard to insert a ‘primary_category’ into the postmeta table that would give you that functionality though.

Viewing 9 replies - 16 through 24 (of 24 total)
  • The topic ‘Trying to find a way to determine the custom post categories from a pageID’ is closed to new replies.