Title: Function using custom SQL to return array of specific category ID
Last modified: August 20, 2016

---

# Function using custom SQL to return array of specific category ID

 *  Resolved [Martin Black](https://wordpress.org/support/users/ownedbyhleb/)
 * (@ownedbyhleb)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/function-using-custom-sql-to-return-array-of-specific-category-id/)
 * I’m developing a custom theme which uses categories for positioning, all of which
   begin with an underscore (e.g. _position1). I have an SQL query as below to get
   a list of these categories (those starting with an underscore).
 *  `SELECT name,term_id FROM ‘wp_terms’
    WHERE name LIKE ‘\_%’;`
 * How would I go about turning this into a function which returns a list of the
   category IDs?
 * The idea being that this list of IDs can be passed to `wp_list_categories()` 
   to be excluded e.g. (where get_positional_ids would use the SQL above to return
   a list of IDs)
 *  `<?php
    $excludeids = get_positional_ids(); $args = array(‘exclude’=> $excludeids);
   wp_list_categories($args); ?>`
 * Any ideas on how to do this?
    Thanks.

Viewing 1 replies (of 1 total)

 *  Thread Starter [Martin Black](https://wordpress.org/support/users/ownedbyhleb/)
 * (@ownedbyhleb)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/function-using-custom-sql-to-return-array-of-specific-category-id/#post-2396051)
 * Solved as follows:
 *     ```
       function get_positional_categories() {
       global $wpdb;
       $poscats = $wpdb->get_results("
           SELECT name,term_id FROM <code>wp_terms</code>
           WHERE name LIKE '\_%';
       ");
       $count = 0;
       foreach ($poscats as $pcat) {
           $poscats[$count] = $pcat->term_id;
           $count++;
       }
       return $poscats;
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Function using custom SQL to return array of specific category ID’ is
closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [list](https://wordpress.org/support/topic-tag/list/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Martin Black](https://wordpress.org/support/users/ownedbyhleb/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/function-using-custom-sql-to-return-array-of-specific-category-id/#post-2396051)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
