查询模板的多个元键值

时间:2014-01-31 作者:user45966

我正在使用此模板:http://demo.undsgn.com/studiofolio/front-page/

我试图进行一些修改,因为我需要主页模板在每个缩略图下显示摘录。我复制了名为front-page.php, 将其重命名为front-page-excerpt.php 我做了一些修改,效果很好。

问题是,它背后有一个框架,控制在主页上显示哪个模板。该框架是:http://aquagraphite.com/2011/09/slightly-modded-options-framework/

在后端,它如下所示:

enter image description hereenter image description here

在后端,我的新模板没有出现,我想我发现了问题。在“后端选项”面板中调用模板,如下所示:

    $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->postmeta.meta_key = \'_wp_page_template\' 
    AND $wpdb->postmeta.meta_value = \'templates/front-page.php\' 
    AND $wpdb->posts.post_status = \'publish\' 
    AND $wpdb->posts.post_type = \'page\'";

    $pageposts = $wpdb->get_results($querystr, OBJECT);
当我将其更改为:

        AND $wpdb->postmeta.meta_value = \'templates/front-page-excerpt.php\'
显示修改后的模板,但不显示原始模板。我试着这样把两者都放进去:

    $querystr = "  
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta AS mt1
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 

    AND $wpdb->postmeta.meta_key = \'_wp_page_template\'   
    AND $wpdb->postmeta.meta_value = \'templates/front-page.php\'
    AND mt1.meta_key = \'_wp_page_template\'
    AND mt1.meta_value = \'templates/front-page-excerpt.php\'

    AND $wpdb->posts.post_status = \'publish\' 
    AND $wpdb->posts.post_type = \'page\'";

    $pageposts = $wpdb->get_results($querystr, OBJECT);
这根本不起作用。我做错了什么?

2 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

不知道为什么要使用笨拙的自定义SQL,请使用meta query 相反

$pageposts = get_posts(
    array(
        \'posts_per_page\' => -1,
        \'post_type\'      => \'page\',
        \'post_status\'    => \'publish\',
        \'meta_query\'     => array(
            array(
                \'key\'   => \'_wp_page_template\',
                \'value\' => array(
                    \'templates/front-page-excerpt.php\',
                    \'templates/front-page.php\',
                )
            )       
        ),  
    )
);

SO网友:Matthieu

如果要同时选择两个模板,则应使用OR和not and运算符

$querystr = "  
SELECT $wpdb->posts.* 
FROM $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta AS mt1
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 

AND ($wpdb->postmeta.meta_key = \'_wp_page_template\'   
OR $wpdb->postmeta.meta_value = \'templates/front-page.php\')
AND mt1.meta_key = \'_wp_page_template\'
AND mt1.meta_value = \'templates/front-page-excerpt.php\'

AND $wpdb->posts.post_status = \'publish\' 
AND $wpdb->posts.post_type = \'page\'";

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

结束