WordPress.org

Ready to get started?Download WordPress

Forums

How to get categories where term taxonomy is 'my_custom_value' (2 posts)

  1. wordyword
    Member
    Posted 5 years ago #

    get_the_category fetches categories from term_taxonomy where taxonomy value is 'category'. But I want to get the categories from term_taxonomy where the taxonomy value is 'my_custom_value' instead of 'category'. Can I modify get_the_category to fetch array from term_taxonomy where taxonomy is 'my_custom_value', or do I need to create a custom query? If a query is the only way, please advise on the best way.

    For the query...this is what I have so far. It works, but does not filter out the terms related to other posts. I can't figure out how to tell the query, "only show me the terms where the posts.ID matches the $post->ID". What do I need to add to this to only get terms related to the post being viewed? Thanks in advance for your help!

    $termquery= $wpdb->get_results
    ("
    SELECT $wpdb->terms.name FROM $wpdb->terms
    INNER JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id
    WHERE $wpdb->term_taxonomy.taxonomy = 'my_custom_value'
    ORDER BY name ASC
    ");
    $displayterms = $wpdb->get_col($termquery['name']);
    echo json_encode($displayterms);
  2. wordyword
    Member
    Posted 5 years ago #

    Update: I'm able to retrieve the category term ID numbers. Now I need to figure out how to retrieve the term names instead of the IDs. Here's what the code result looks like:

    displayterms = ["24","25"];

    And here's the code. Where do I need to change the following code so that names are retrieved instead of ID #'s?

    $termquery = get_the_category
    ("
    SELECT $wpdb->name FROM terms
    WHERE $wpdb->term_taxonomy.taxonomy = 'my_custom_value'
    ");
    
    $displayterms = $wpdb->get_col($termquery['$name']);
    echo json_encode($displayterms);

Topic Closed

This topic has been closed to new replies.

About this Topic