Select post based on category name (not cat id)? (6 posts)

  1. bitbutter
    Posted 10 years ago #

    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!

  2. yas
    Posted 10 years ago #

    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?

  3. bitbutter
    Posted 10 years ago #

    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 :)

  4. yas
    Posted 10 years ago #

    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;


  5. gotjosh
    Posted 10 years ago #

    thanks yas for a good start...
    i adapted your code by looking here:

    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)


  6. eric_ie
    Posted 9 years ago #


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

Topic Closed

This topic has been closed to new replies.

About this Topic