【问题标题】:Use more than one post meta inside the loop在循环中使用多个帖子元
【发布时间】:2015-03-03 15:02:35
【问题描述】:

我有自定义元 st_date 和 end_date

我想选择满足这两个条件的帖子:

1- end_date = "某个日期"

2- st_date = "某个日期"

3-我还需要按标题选择

4-然后在 WordPress 循环中使用结果。

if ( have_posts() ) : ?>
<?php get_template_part('loop'); ?> //etc..

这是我的草稿代码:

$st_date_s = 'something';
$end_date_s = 'something';
$pageposts = get_posts(
    array(
        'post_title'      => 'something'
        'post_type'      => 'something',
        'post_status'    => 'publish',
        'meta_query'     => array(
            array(
                array( 'key' => 'st_date', 'value' => $st_date_s, 'compare' => '=' ), 
                array( 'key' => 'end_date', 'value' => $end_date_s, 'compare' => '=' )
            )       
        ),  
    )
);
?>
<?php global $site_url;
if ( have_posts() ) : ?>
<?php get_template_part('loop'); ?>
<?php else : ?>//etc..

还有为什么 'post_title'=> 不起作用?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您缺少relation 参数,并且有太多array()s。由于您希望这两个条件都为真,您应该使用AND

    ...
    
        'meta_query'     => array(
            'relation' => 'AND',
            array( 'key' => 'st_date', 'value' => $st_date_s, 'compare' => '=' ), 
            array( 'key' => 'end_date', 'value' => $end_date_s, 'compare' => '=' ),
        ),  
    
    ...
    

    阅读更多关于custom field parameters in the docs的信息。

    【讨论】:

    • 我删除了多余的数组并使用了 'relation' => 'AND'。
    • 是的,但是 'post_title' => 还不行,我还在测试你的解决方案的其余部分
    • 啊,没有可用的post_title 参数...但是您可以使用get_page_by_title() 获取帖子ID 并将其传递给对象。
    猜你喜欢
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 2013-01-21
    • 2013-11-23
    相关资源
    最近更新 更多