WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: My Category Order] use it manually (5 posts)

  1. mores
    Member
    Posted 5 years ago #

    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.

  2. kickass
    Member
    Posted 5 years ago #

    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?

  3. froman118
    Member
    Posted 5 years ago #

    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.

  4. mores
    Member
    Posted 4 years ago #

    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?

  5. mores
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic