Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Why doesn’t this Custom Select Query work?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #32886
    Luminated
    Member

    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.

    #75234
    TheDoc
    Member

    Forgive my ignorance, but is there a specific reason why you have to use the Select Query instead of the regular query_posts in this case?

    #75235
    Luminated
    Member

    I am actually using this within the Custom Field Template plugin. The situation is I basically want a pre-populated list of posts as a drop-down that the user will be able to select the associated post and it will automatically update the permalink for this particular custom field.

    Within the Custom Field Template plugin, you have the ability to use PHP Code to hook into the Database and pull whatever you need from it. I don’t think query_posts will do what I want it to.

    I posted about this before here:

    https://css-tricks.com/forums/discussion/8004/custom-field-wordpress-that-will-auto-populate-from-others/p1

    I got it to work great and now I am basically trying to do the same thing, but I want to relegate the query to only posts within a certain category.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Other’ is closed to new topics and replies.