WP Query Meta Query

时间:2017-08-06 作者:user100754

我需要根据带有特定键的两个自定义字段选择要显示的帖子,并按第三个字段排序。这是我的产品,但订购不正确。

    <?php
$args           = array(
    \'post_type\' => \'event\',
    \'order\' => \'ASC\',
    \'meta_query\' => array(
        \'key\' => \'Date Ordering Format\',
        \'orderby\' => \'meta_value\',
        \'relation\' => \'AND\',
        array(
            \'key\' => \'Event Type\',
            \'value\' => \'4. take-out tuesday\',
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\' => \'Past Event\',
            \'value\' => \'Past\',
            \'compare\' => \'EXISTS\'
        )
    )
);
$featured_query = new WP_Query($args);
if ($featured_query->have_posts()):
    while ($featured_query->have_posts()):
        $featured_query->the_post();
        echo \'<div class="fullWidthSeason">\';
        echo \'<div class="pastEventImage">\';
        if (class_exists(\'MultiPostThumbnails\')):
            MultiPostThumbnails::the_post_thumbnail(get_post_type(), \'series-image\', $post->ID, \'full\', NULL, FALSE);
        endif;
        echo \'</div>\';
        echo \'<div class="featuredBlockDate \' . get_post_meta($post->ID, \'Event Text Left or Right\', true) . \' \' . get_post_meta($post->ID, \'Black Text\', true) . \' \' . get_post_meta($post->ID, \'Black Text\', true) . \'">\';
        echo \'Past Event: \';
        echo get_post_meta($post->ID, \'Event Date\', true);
        echo \'</div>\';
        echo \'<div class="featuredBlockTitle  \' . get_post_meta($post->ID, \'Event Text Left or Right\', true) . \' \' . get_post_meta($post->ID, \'Black Text\', true) . \'">\';
        echo get_post_meta($post->ID, \'Season Page Title\', true);
        echo \'</div>\';
        echo \'<div class="featuredBlockExcerpt  \' . get_post_meta($post->ID, \'Event Text Left or Right\', true) . \' \' . get_post_meta($post->ID, \'Black Text\', true) . \'">\';
        echo the_excerpt();
        echo \'</div>\';
        echo \'<div class="moreButton  \' . get_post_meta($post->ID, \'Event Text Left or Right\', true) . \'">\';
        echo \'<div class="moreButtonSeason  \' . get_post_meta($post->ID, \'Event Text Left or Right\', true) . \'"><a href="\' . get_post_meta($post->ID, \'Event RSVP Link\', true) . \'" target="_blank">\' . get_post_meta($post->ID, \'Event RSVP Link Text\', true) . \'</a></div>\';
        echo \'</div>\';
        echo \'</div>\';
    endwhile;
    wp_reset_query();
endif;
?>

1 个回复
SO网友:Pascal Knecht

我想orderby键放错地方了。它认为应该是这样的:

$args           = array(
    \'post_type\' => \'event\',
    \'order\' => \'ASC\',
    \'orderby\' => \'meta_value\',
    \'meta_key\' => \'your key to order\',
    \'meta_query\' => array(
        \'key\' => \'Date Ordering Format\',
        \'relation\' => \'AND\',
        array(
            \'key\' => \'Event Type\',
            \'value\' => \'4. take-out tuesday\',
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\' => \'Past Event\',
            \'value\' => \'Past\',
            \'compare\' => \'EXISTS\'
        )
    )
);

结束

相关推荐

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

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