WordPress.org

Support

Support » Plugins and Hacks » Hacks » Get Post By Slug custom function help

Get Post By Slug custom function help

  • Please help me with my custom function “get_post_by_slug”, which tries to query the database and get post id where slug is LIKE something% from a specific category (id=5). It worked until WP 3.5, but it is broken now, I think there could be a problem with the cache. I added the $wpdb->prepare for WP 3.5 but it didn’t really fix the problem. If it is simpler to make a function that return slug where slug is LIKE word%, then that is acceptable solution as well.


    function get_post_by_slug($post_slug) {
    global $wpdb;
    $post = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id WHERE post_name LIKE '%s%' AND term_taxonomy_id=5 LIMIT 1", $post_slug));
    return $post;
    }

    Thanks in advance!

  • The topic ‘Get Post By Slug custom function help’ is closed to new replies.
Skip to toolbar