• I’m using a Custom Select Query, attempting to get all posts under a certain category. Here is what I have:

    global $wpdb;
    $items = $wpdb->get_results("SELECT ID, post_title
    FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
    WHERE post_type = 'post'
    AND post_status = 'publish'
    WHERE $wpdb->term_taxonomy.term_id = 4
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    order by post_title ASC");
    $i = 0;
    foreach ($items as $item) {
                    $values[$i] = $permalink = get_permalink($item->ID);
                    $valueLabel[$i] = $item->post_title;
                    $i++;
    }

    This was my original query which works fine in pulling ALL Posts that are Published:

    global $wpdb;
    $items = $wpdb->get_results("SELECT ID, post_title
    FROM $wpdb->posts
    WHERE post_type = 'post'
    AND post_status = 'publish'
    order by post_title ASC");
    $i = 0;
    foreach ($items as $item) {
                    $values[$i] = $permalink = get_permalink($item->ID);
                    $valueLabel[$i] = $item->post_title;
                    $i++;
    }

    But I want to pull all Published Posts under the Category of “weddings” (or ID 4). Why is this query not working? Not sure what I am missing here.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Why doesn't this Custom Select Query work?’ is closed to new replies.