• about the get_posts() fuction:

    Is there any way to specify the category to get posts from by passing a category name rather than id?

    Or alternatively is there a function that can be used to return the category id if a category name is passed to it?

    thanks for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • That function isn’t built into WordPress, but it should be simple enough to write plugin or script that does that.

    Are you good with PHP?

    Thread Starter bitbutter

    (@bitbutter)

    yeah i have a few years intermediate php experience.. but i’m not sure where to begin with modifying the wp code. thanks for your repy.. i’ll go poking around.. if anyone has any pointers i’m all ears ??

    It would be something like this:

    function get_cat_id_using_name($catname) {
    global $wpdb;

    $request = $wpdb->get_results(“SELECT cat_ID FROM $wpdb->categories WHERE cat_name = ‘$catname’ “);

    return $request->cat_ID;

    }

    gotjosh

    (@gotjosh)

    thanks yas for a good start…
    i adapted your code by looking here:
    https://codex.www.ads-software.com/Function_Reference/wpdb_Class#get_var_-_SELECT_a_Variable

    and ended up with this working fx:

    function get_catid($catname){
    global $wpdb;
    $request = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name = '$catname' ");
    return $request;
    }
    $stickyid = get_catid('sticky');

    i do think such a thing should be included in the main
    in_category() fx… it can check if it is passed numbers and if not do a quick lookup using this kind of function… seemlessly handling both names and id’s…
    i can hack and contribute this, if noone else is working on it… just need to figure out how to do that contribution thing around here (a bit new to the community)

    cheers,
    gotjosh

    gotjosh,

    Do you know if that was implemented in the WP 2.0 release? I am in need of something similar but a little simpler.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Select post based on category name (not cat id)?’ is closed to new replies.