Support » Plugins and Hacks » [Plugin: My Category Order] use it manually

  • My client suddenly decided to rearrange his categories, and of course, neither ID nor NAME would get the required results.
    So I installed My Category Order to get the job done.
    But, since I didn’t use wp_list_categories() but cycled through the categories manually (needed descriptions, that’s why), I had to change some code to make use of the cool plugin. Old code:

    <?php
    global $wpdb;
    $i=0;
    $categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE parent = '$cat' ORDER BY name ASC");
    foreach($categories as $category) :?>
    	<h1><a href="?cat=<?php echo $category->id; ?>"><?php echo $category->name; ?></a></h1>
    	<?php echo $category->description; ?>
    
    <?php endforeach; ?>

    New code:

    <?php
    global $wpdb;
    $i=0;
    $categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description, term_order from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE parent = '$cat' ORDER BY term_order ASC");
    foreach($categories as $category) :?>
    	<h1><a href="?cat=<?php echo $category->id; ?>"><?php echo $category->name; ?></a></h1>
    	<?php echo $category->description; ?>
    
    <?php endforeach; ?>

    Note: I’m using term_order to sort.

Viewing 4 replies - 1 through 4 (of 4 total)
  • This worked a treat! It would be perfect if I knew how would I write this to exclude the link category(s). Any help for that?

    If you use the query above verbatim then you could include a “NOT IN(1,2,3,4)” statement in the where clause, replacing the list of numbers with the id’s you want to exclude. Might look something like this:

    $categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description, term_order from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE parent = '$cat' AND id not in(1,2,3,4) ORDER BY term_order ASC");

    That should work.

    unfortunately, that does not work.
    I just now needed to exclude some categories from the query and your solution does not work. Shows nothing at all. Any ideas why?

    Ah, found it.
    Must be
    $wpdb->terms.term_id not in(6,9,10,12)
    instead of
    id not in(1,2,3,4)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: My Category Order] use it manually’ is closed to new replies.