• Resolved Martin Black

    (@ownedbyhleb)


    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

    (@ownedbyhleb)

    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.