• Resolved matt_ny

    (@matt_ny)


    Add condition to search only for posts in specific category(ies) within this function?

    function post_exist($title) {
    global $wpdb;
    $post_slug = stripslashes(sanitize_post_field('post_name', $title, 0, 'db'));
    $query = "SELECT ID FROM $wpdb->posts WHERE 1=1 AND post_status = 'publish' AND post_type = 'post'";
    $args = array();
    if (!empty ($title)) {
    $query .= ' AND post_name = %s';
    $args[] = $post_slug;
    }
    if (!empty ($args))
    return $wpdb->get_var($wpdb->prepare($query, $args));
    return 0;
    }

    Does this function too bad for CPU performance?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Can’t speak to performance but using your function with another query:

    <?php
    $postid=post_exist('new-post');
    $cat_id = get_cat_ID('Uncategorized');
    $args=array(
      'category__in' => array($cat_id),
      'post__in'=> array($postid),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    ?>

    This is the SQL WordPress uses for that new WP_Query:

    SELECT   wp_posts.* FROM wp_posts
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
    WHERE 1=1  AND wp_posts.ID IN (476)
    AND wp_term_taxonomy.taxonomy = 'category'
    AND wp_term_taxonomy.term_id IN ('1')  AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish')
    GROUP BY wp_posts.ID
    ORDER BY wp_posts.post_date DESC

    Thread Starter matt_ny

    (@matt_ny)

    thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to – add condition to search only for posts in specific category(ies)’ is closed to new replies.