QUERY_POST:如何显示包含特定单词的所有‘META_VALUE’?

时间:2011-05-31 作者:Rob

如何使用包含单词/短语的(自定义)meta\\u值在循环中显示所有帖子?例如,获取所有meta包含“test”的帖子。

query_posts(\'post_type=SOME&category_name_2=SOME&meta_value= \'Cotains the word \'test\' ??? \'); 
因此,当元标记是“测试电影”或只是“测试”时,您可以同时获得它们。?

2 个回复
SO网友:TheDeadMedic

无需自定义查询。从3.1开始,您可以使用“元查询”;

query_posts( array(
    \'meta_query\' => array(
        array( 
            \'key\' => \'meta_key_name\',
            \'value\' => \'test\',
            \'compare\' => \'LIKE\'
        )
    )
) );
退房the codex on custom field parameters.

SO网友:Boldhand

使用“自定义选择查询”

<?php

 $querystr = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id 
    AND wpostmeta.meta_key = \'tag\' 
    AND wpostmeta.meta_value LIKE \'%test%\' 
    AND wposts.post_status = \'publish\' 
    AND wposts.post_type = \'post\'  
    ORDER BY wposts.post_date DESC
 ";

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

?>
 <?php if ($pageposts): ?>
  <?php global $post; ?>
  <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
  <?php endforeach; ?>
 <?php endif; ?>

结束

相关推荐