Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter SlightlyAmiss

    (@slightlyamiss)

    This was extremely helpful on a per-post basis. Not quite what I was looking for but will get me to my goal.

    <?php $parentcat = get_category_by_slug('system');
    foreach((get_the_category()) as $childcat):
      if (cat_is_ancestor_of($parentcat, $childcat)):
        echo $childcat->cat_ID;
      endif;
    endforeach; ?>

    I use multiple categories for every post, with this code I can input the parents slug in the first line and it will echo back out the corresponding sub category ID.

    Hope this can help someone!

    Thread Starter SlightlyAmiss

    (@slightlyamiss)

    The following block of code gets me a lot closer to where I want to be:

    <?php
    $category_ids = get_all_category_ids();
    foreach($category_ids as $cat_id) {
      $cat_name = get_cat_name($cat_id);
      echo $cat_id . ', ';
    }
    ?>

    Is there some way to stipulate that I only want the category IDs to be children of parent cat, in my case 31?

    Thanks!

    Thread Starter SlightlyAmiss

    (@slightlyamiss)

    Bump. Anyone have any ideas. This is either really simple or a complete brain buster. Somehow I doubt it’s the latter.

    Any help would be greatly appreciated! Thanks!

    Thread Starter SlightlyAmiss

    (@slightlyamiss)

    I figured out how to modify the first block of code in my original post to output only sub-categories of a specified ID. I’ll post it here in case anyone needs reference.

    <?php
    $categories = $wpdb->get_results("
    	SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug
    	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 1=1 AND (
    		posts.post_status = 'publish' AND
    		posts.post_author = '4' AND
    		tax.taxonomy = 'category' AND
                    tax.parent = '29'
                    )
    	ORDER BY terms.name ASC
    ");
    ?>
    <ul>
    	<?php foreach($categories as $category) : ?>
    	<li>
    		<a href="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>"><?php echo $category->name ?></a>
    	</li>
    	<?php endforeach; ?>
    </ul>

    Change the tax.parent value to the parent category’s ID. In my case, the list of sub-categories all needed to spawn from parent category 29.

    Hope this helps!

    Thread Starter SlightlyAmiss

    (@slightlyamiss)

    Here’s an additional piece of code that I came across which correctly outputs a list of categories with a specific parent ID.

    $parent = 29;// your category parent ID
    $where = "AND tt.parent = '$parent'";
    $in_taxonomies = "'category'";
    $orderby = 't.term_id';
    $order = 'ASC';
    $limit = 'LIMIT 0,100';
    
    $query = "SELECT t.name FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $limit";

    However, it’s not author-centric. Is there a way to combine some of the rules of the first and second queries I’ve found to achieve something author centric?

Viewing 5 replies - 1 through 5 (of 5 total)