Forums

Custom Query Returns Nothing? (4 posts)

  1. leafmealone
    Member
    Posted 3 years ago #

    WHYYY in God's name does the following custom query not return any posts? It is intended to grab all posts out of category '9' and sort them by date (a meta field, not post date).

    ` $querystr =
    "
    SELECT $wpdb->posts.*
    FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
    LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id)
    WHERE $wpdb->postmeta.meta_key = 'date'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->post2cat.category_id IN (9)
    ORDER BY $wpdb->postmeta.meta_value DESC
    ";

    $pageposts = $wpdb->get_results($querystr, OBJECT);

    if ($pageposts):

    foreach ($pageposts as $post):

    setup_postdata($post);`

    It is taken practically right out of a Codex example. I can't figure it out, I've been combing through it all day?

  2. Michael Fields
    Theme Wrangler
    Posted 3 years ago #

    One things for sure... WordPress 2.6 does not have a 'post2cat' table. You must be reading an outdated codex page, can you provide a link to it?

  3. leafmealone
    Member
    Posted 3 years ago #

    Hmm. That most likely is the problem...what is the newest way to achieve this?

    Here is the link. The example I copied was towards the bottom:
    Displaying Posts Using A Custom Select query

    Any help would be much appreciated.

  4. Michael Fields
    Theme Wrangler
    Posted 3 years ago #

    I just visited the page. Have you tried the query directly below the query that you tried to use? It looks like it is set up to work with the "new" taxonomy tables.

Topic Closed

This topic has been closed to new replies.

About this Topic