WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] specific custom taxonomy terms by category id (2 posts)

  1. Taylor Baybutt
    Member
    Posted 4 years ago #

    This is an offshoot of another thread here: http://wordpress.org/support/topic/276635/page/2?replies=39

    I am trying to get the terms of a custom taxonomy called 'media' by a category. I have been toiling over this and just returned my code back to the way it was. I don't see why this is not working but it seems to me that terms within a custom taxonomy either do not have the term_link term_name and term_id tables or that they will not be returned using the get_tag_link function.

    This is probably the deepest I have ventured into sql queries so any help is much aprpeciated. Here is the function followed by my template code. please note that my tables do not use the default wp_ prefix.

    function get_category_media($args) {
    	global $wpdb;
    	$tags = $wpdb->get_results
    	("
    		SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
    		FROM
    			dorightby_posts as p1
    			LEFT JOIN dorightby_term_relationships as r1 ON p1.ID = r1.object_ID
    			LEFT JOIN dorightby_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
    			LEFT JOIN dorightby_terms as terms1 ON t1.term_id = terms1.term_id,
    
    			dorightby_posts as p2
    			LEFT JOIN dorightby_term_relationships as r2 ON p2.ID = r2.object_ID
    			LEFT JOIN dorightby_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
    			LEFT JOIN dorightby_terms as terms2 ON t2.term_id = terms2.term_id
    		WHERE
    			t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
    			t2.taxonomy = 'media' AND p2.post_status = 'publish'
    			AND p1.ID = p2.ID
    		ORDER by tag_name
    	");
    	$count = 0;
    	foreach ($tags as $tag) {
    		$tags[$count]->tag_link = get_tag_link($tag->tag_id);
    		$count++;
    	}
    	return $tags;
    }
    <?php
    $mediaargs = array(
    	'categories'				=> '30'
    );
    $tags = get_category_media($mediaargs);
    foreach ($tags as $tag) {
    	$content = "<a href=\"$tag->term_link\">$tag->term_name</a>";
    }
    echo $content;
    ?>
  2. Taylor Baybutt
    Member
    Posted 4 years ago #

    ok, i got this working, finally! the problem came because the "tag_link" was only retrieving the base url of the custom taxonomy term but not the url. I added a query to retrieve the slug and then just add it on to the printed content within my template. Here is the code, ready to be inserted into your custom taxonomy equipped theme.

    in functions.php add

    function get_category_media($args) {
    	global $wpdb;
    	$tags = $wpdb->get_results
    	("
    		SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, terms2.slug as tag_slug, null as tag_link
    		FROM
    			wp_posts as p1
    			LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
    			LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
    			LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,
    
    			wp_posts as p2
    			LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
    			LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
    			LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
    		WHERE
    			t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
    			t2.taxonomy = 'media' AND p2.post_status = 'publish'
    			AND p1.ID = p2.ID
    		ORDER by tag_name
    	");
    	$count = 0;
    	foreach ($tags as $tag) {
    		$tags[$count]->tag_link = get_term_link($tag, 'media');
    		$count++;
    	}
    	return $tags;
    }

    (if you are running multiple wordpress installs on your server, you probably changed the 'wp' table prefix so make sure about that.)

    insert into your category template (and alter as needed):

    $category = get_the_category();
    $category_id = $category[0]->cat_ID;
    $args = array(
    	'categories'				=> $category_id
    );
    $tags = get_category_tags($args);
    $content .= "
    
    <ul>";
    foreach ($tags as $tag) {
    	$content .= "
    
    <li><a>tag_link$tag->tag_slug\">$tag->tag_name</a></li>
    ";
    }
    $content .= "</ul>
    ";
    echo $content;

    this probably could use to be cleaned up seeing as it is the first time ive really delved this deep into an SQL query. But it is doing the job, so I hope it works for you.

Topic Closed

This topic has been closed to new replies.

About this Topic