具有自定义发布类型的自定义选择查询

时间:2011-02-08 作者:Noel Tock

在过去的一个小时里,我一直在试图解决这个问题,但遇到了麻烦。基本上,我只是尝试根据一些自定义日期字段查询我的自定义帖子类型,然后显示每个字段。有人知道我哪里出错了吗?

<?php    
// - query -
                 global $wpdb;
                 $yesterday = strtotime("-1 day");
                 $querystr = "
                    SELECT *
                    FROM $wpdb->posts
                    INNER JOIN {$wpdb->postmeta}postmeta m1
                      ON ( {$wpdb->posts}posts.ID = m1.post_id )
                    INNER JOIN {$wpdb->postmeta}postmeta m2
                      ON ( {$wpdb->posts}posts.ID = m2.post_id )
                    WHERE $wpdb->posts.post_type = \'tf_events\'
                    AND $wpdb->posts.post_status = \'publish\'
                    AND ( m1.meta_key = \'tf_events_startdate\' AND m1.meta_value > $yesterday )
                    AND ( m2.meta_key = \'tf_events_starttime\')
                    ORDER BY m1.meta_key ASC, m2.meta_key ASC LIMIT 30
                 ";

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

                if ($events):
                global $post;
                foreach ($events as $post):
                setup_postdata($post);

                // - variables -
                $custom = get_post_custom(get_the_ID());
                $sd = $custom["tf_events_startdate"][0];
                $ed = $custom["tf_events_enddate"][0];
                $st = $custom["tf_events_starttime"][0];
                $et = $custom["tf_events_endtime"][0];
                $post_image_id = get_post_thumbnail_id(get_the_ID());
                        if ($post_image_id) {
                                $thumbnail = wp_get_attachment_image_src( $post_image_id, \'post-thumbnail\', false);
                                if ($thumbnail) (string)$thumbnail = $thumbnail[0];
                        }

                // - output -
                ?>

                    <div>Show the goodies</div>

              <?php endforeach; ?>

              <?php else : ?>
                <h2 class="center">Not Found</h2>
                <p class="center">Sorry, but you are looking for something that isn\'t here.</p>
              <?php endif; ?>
提前感谢:)

3 个回复
最合适的回答,由SO网友:Ethan Seifert 整理而成

我会检查$querystr. 看起来您有两次表名。$wpdb->postmeta 下定决心wp_postmeta 然后你有postmeta 再一次这给了你wp_postmetapostmeta 这不是有效的表。

我强烈建议你试试Debug Bar. 它大大简化了查询调试。

SO网友:kaiser

接近此查询(不知道您是否可以选择多个元键-orderby也可以):

$tf_events_query = new WP_Query( array(
     \'post_type\'        => \'tf_events\'
    ,\'posts_per_page\'   => \'30\'
    // ordered by meta_valu AND date
    ,\'orderby\'          => \'meta_value date\'
    // or if the meta key value is numeric ordered by num meta value AND date
    // ,\'orderby\'       => \'meta_value_num date\'
    ,\'meta_key\'         => \'tf_events_startdate\'
    ,\'order\'            => \'ASC\'
) );

SO网友:Noel Tock

终于明白了,谢谢大家的帮助。。。

<?php
        $querystr = "
            SELECT *
            FROM $wpdb->posts wposts, $wpdb->postmeta metadate, $wpdb->postmeta metatime
            WHERE (wposts.ID = metadate.post_id AND wposts.ID = metatime.post_id)
            AND (metadate.meta_key = \'tf_events_startdate\' AND metadate.meta_value > $yesterday )
            AND metatime.meta_key = \'tf_events_starttime\'
            AND wposts.post_type = \'tf_events\'
            AND wposts.post_status = \'publish\'
            ORDER BY metadate.meta_value ASC, metatime.meta_value DESC LIMIT 30
         ";
?>

结束