WordPress.org

Ready to get started?Download WordPress

Forums

SQL SELECT ~ How to detect parent / children categories? (1 post)

  1. 2famous
    Member
    Posted 1 year ago #

    This code gives me all the categories, but how can I filter out parent / children?

    <?php
    	$categories = $wpdb->get_results("
        SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
        FROM $wpdb->posts as posts
        LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
        LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
        LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
        WHERE posts.post_status = 'publish' AND
        	posts.post_author IN ($author) AND
            tax.taxonomy = 'category'
        ORDER BY terms.name ASC
        ");
    
    echo $category->name;
    
        ?>

Topic Closed

This topic has been closed to new replies.

About this Topic