wordpress自定义查询与元数据

时间:2012-04-16 05:13:00

标签: wordpress wordpress-theming

我正在尝试获取带有元标题标题的最新帖子,但它给了我最新的帖子。这是我的疑问,我做错了什么?

$querydetails = "
        SELECT $wpdb->posts.* 
        FROM $wpdb->posts, $wpdb->postmeta
        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id  
        AND $wpdb->postmeta.meta_value = 'headline'
        AND $wpdb->postmeta.meta_key = 'custom_select'
        AND $wpdb->posts.post_status = 'publish'
        AND $wpdb->posts.post_type = 'post'
        ORDER BY $wpdb->posts.post_date DESC
        LIMIT 1
    ";

    $headline = $wpdb->get_results($querydetails, OBJECT);

1 个答案:

答案 0 :(得分:1)

您应该在wp_query中执行此操作 下面的代码应该这样做。

<?php
$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'meta_key' => 'custom_select',
    'meta_value' => 'headline',
    'posts_per_page' => '1', //limit
    'paged' => get_query_var( 'page' ),
    'order' => 'DESC',
    'orderby' => 'date'
);
$query = new WP_Query($args);
?>

这样您就可以在loop

中使用默认的wordpress标记,例如the_content()