在wpdb中获取年份和日期

时间:2013-09-27 作者:Harish Chouhan

我正在创建一个基于某些选项显示数据的短代码。

function dot_irt_top_posts ( $atts, $content = null ) {

    // get our variable from $atts
    extract(shortcode_atts(array(
        \'before\' => \'<li>\',
        \'after\' => \'</li>\',
        \'number\' => \'10\',
        \'post_type\' => \'post\',
        \'year\' => \'\',
        \'monthnum\' => \'\',
        \'show_count\' => \'1\',
    ), $atts));

    global $wpdb;

    $request = "SELECT * FROM $wpdb->posts, $wpdb->postmeta";
    $request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";

    if ($year != \'\') {
        $request .= " AND year=$year";
    }

    if ($year != \'\' && $monthnum != \'\') {
        $request .= " AND &monthnum=\'$monthnum\'";
    } elseif ($monthnum != \'\') {
        $request .= " AND monthnum=\'$monthnum\'";
    }

    $request .= " AND post_status=\'publish\' AND post_type=\'$post_type\' AND meta_key=\'_recommended\'";
    $request .= " ORDER BY $wpdb->postmeta.meta_value+0 DESC LIMIT $number";
    $posts = $wpdb->get_results($request);

    $return = \'\';


    foreach ($posts as $item) {
        $post_title = stripslashes($item->post_title);
        $permalink = get_permalink($item->ID);
        $post_count = $item->meta_value;
        $return .= $before.\'<a href="\' . $permalink . \'" title="\' . $post_title.\'" rel="nofollow">\' . $post_title . \'</a> \';

        if ( $show_count == \'1\') {
            $return .= \'<span class="votes">\' . $post_count . \'</span> \';
        }

        //$return .= get_the_post_thumbnail($item->ID, \'showcase-thumbnail\');
        $return .= $after;

    }
    return $return;

}
add_shortcode(\'irt_top_posts\',\'dot_irt_top_posts\');
然后,短代码如下:

<?php echo do_shortcode("[irt_top_posts post_type=\'showcase\' number=\'10\' monthnum=\'9\']"); ?>

UPDATED QUESTION\'s

<我的查询似乎有问题。法典http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters 有一个使用WP\\U查询时使用日期检索结果的示例。我想知道在获取结果时,我需要对代码进行哪些更改才能在查询中包含年份或月份。我注意到,在与数据库通信时,大多数短代码都使用wpdb类而不是wp\\U查询。这背后有什么原因吗

2 个回复
SO网友:s_ha_dum

你提到了WP_Query 但您没有使用WP_Query. 您正在使用wpdb.

WP_Query 是一个非常复杂的类,用于查询WordPress表中的post(CPT)数据。wpdb 是PHP SQL函数的轻量级包装器。具有WP_Query 您可以传递如下参数monthnum 因为WP_Query 类将参数转换为正确的SQL。

具有wpdb <您必须编写SQL。$wpdb->get_results 本质上是将SQL传递给数据库引擎。如果你看看database descriptions, 没有名为的列monthnum. 您要求MySQL从不存在的列中提取数据。这就是为什么会出现错误。

您需要编写有效的SQL--look at the date manipulation functions-- 或使用WP_Query 你已经在调查谁的法典页了。后者是更好的选择。

SO网友:Harish Chouhan

终于从另一个帖子得到了答案Custom $wpdb Query for Custom Post Type by Category:

function dot_irt_top_posts ( $atts, $content = null ) {

// get our variable from $atts
extract(shortcode_atts(array(
    \'before\' => \'<li>\',
    \'after\' => \'</li>\',
    \'number\' => \'10\',
    \'post_type\' => \'post\',
    \'year\' => \'\',
    \'monthnum\' => \'\',
    \'show_count\' => \'1\',
), $atts));

global $wpdb;

$request = "SELECT * FROM $wpdb->posts, $wpdb->postmeta";
$request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";

if ($year != \'\') {
    $request .= " AND YEAR(post_date) = \'$year\'";
}

if ($monthnum != \'\') {
    $request .= " AND MONTH(post_date) = \'$monthnum\'";
}

$request .= " AND post_status=\'publish\' AND post_type=\'$post_type\' AND meta_key=\'_recommended\'";
$request .= " ORDER BY $wpdb->postmeta.meta_value+0 DESC LIMIT $number";
$posts = $wpdb->get_results($request);

$return = \'\';


foreach ($posts as $item) {
    $post_title = stripslashes($item->post_title);
    $permalink = get_permalink($item->ID);
    $post_count = $item->meta_value;
    $return .= $before.\'<a href="\' . $permalink . \'" title="\' . $post_title.\'" rel="nofollow">\' . $post_title . \'</a> \';

    if ( $show_count == \'1\') {
        $return .= \'<span class="votes">\' . $post_count . \'</span> \';
    }

    //$return .= get_the_post_thumbnail($item->ID, \'showcase-thumbnail\');
    $return .= $after;

}
return $return;

}
add_shortcode(\'irt_top_posts\',\'dot_irt_top_posts\');

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post