Forums

[resolved] Getting Tags using mySQL query (3 posts)

  1. kthakk4
    Member
    Posted 5 months ago #

    There is a wp plugin that displays lists categories in a multiple columns on a page. I want to modify this plugin so as to do the same with tags instead of category.

    The original code (with category) uses the following query to obtain categories:

    $cat_list = (array)$wpdb->get_results("
    			SELECT {$tp}terms.term_id as cat_ID,
    				{$tp}terms.name as cat_name
    			FROM {$tp}terms, {$tp}term_taxonomy, {$tp}term_relationships, {$tp}posts
    			WHERE {$tp}term_taxonomy.taxonomy = 'category'
    			AND {$tp}term_taxonomy.term_id = {$tp}terms.term_id
    			AND {$tp}term_taxonomy.term_taxonomy_id = {$tp}term_relationships.term_taxonomy_id
    			AND {$tp}term_relationships.object_id = {$tp}posts.ID
    			AND {$tp}terms.term_id = {$tp}term_taxonomy.term_id
    			{$exclude_sql}
    			GROUP BY cat_name
    			ORDER BY cat_name
    		");

    Obviously, my intuition to replace all the cat references with tag reference and change the code. This is my modified code:

    $tag_list = (array)$wpdb->get_results("
    			SELECT {$tp}terms.term_id as tag_ID,
    				{$tp}terms.name as tag_name
    			FROM {$tp}terms, {$tp}term_taxonomy, {$tp}term_relationships, {$tp}posts
    			WHERE {$tp}term_taxonomy.taxonomy = 'tag'
    			AND {$tp}term_taxonomy.term_id = {$tp}terms.term_id
    			AND {$tp}term_taxonomy.term_taxonomy_id = {$tp}term_relationships.term_taxonomy_id
    			AND {$tp}term_relationships.object_id = {$tp}posts.ID
    			AND {$tp}terms.term_id = {$tp}term_taxonomy.term_id
    			{$exclude_sql}
    			GROUP BY tag_name
    			ORDER BY tag_name
    		");

    However this results in a null string. Any idea how I can fix this?

    Thanks

  2. kthakk4
    Member
    Posted 5 months ago #

    I figured it out. In the following line, I needed 'post_tag' instead of just 'tag'

    WHERE {$tp}term_taxonomy.taxonomy = 'tag'
  3. aborisov
    Member
    Posted 5 months ago #

    What plugin you are talking about?

    I've had two plugins for tags but since I've upgraded to WP 2.8 none of that plugins are working.

Reply

You must log in to post.

About this Topic

Tags