WordPress.org

Ready to get started?Download WordPress

Forums

Get Post By Slug custom function help (1 post)

  1. return0
    Member
    Posted 1 year ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic