具有多个元键值的自定义查询

时间:2014-03-04 作者:Manan

我是WordPress开发的新手,我找到了这个查询,我想在这个查询中传递多个键和值。

我的问题是:

$querydetails = "
   SELECT wposts.*
   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = \'type\' AND wpostmeta.meta_value = \'Collection1\'
   AND wposts.post_status = \'publish\'
   AND wposts.post_type = \'book\'
   ORDER BY wposts.post_date DESC
";
我想添加更多语句OR 操作人员

我的原始代码是:

<?php
$querydetails = "
   SELECT wposts.*
   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = \'type\' AND wpostmeta.meta_value = \'Collection1\'
   AND wposts.post_status = \'publish\'
   AND wposts.post_type = \'book\'
   ORDER BY wposts.post_date DESC
";

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

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

   <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

<?php endforeach;
endif; ?>
我也尝试过这段代码,但当我传递4条记录时,什么都没有发生,只有页面正在加载和加载。

<?php   
$args = array(


    //Type & Status Parameters
    \'post_type\'   => \'book\',
    \'post_status\' => \'publish\',

    //Order & Orderby Parameters
    \'order\'               => \'DESC\',
    \'orderby\'             => \'date\',

    //Pagination Parameters
    \'posts_per_page\'         => -1,
            // Here you can add you second meta value

    \'meta_query\'     => array(
                   \'relation\' => \'OR\',
        array(
            \'key\' => \'type\',
            \'value\' => \'Sample 1\',
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\' => \'type\',
            \'value\' => \'Sample 2\',
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\' => \'type\',
            \'value\' => \'Sample 3\',
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\' => \'type\',
            \'value\' => \'Sample 4\',
            \'compare\' => \'LIKE\'
        )
    )
);

$query = new WP_Query( $args );
?>

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<h2><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h2>


<?php endwhile; ?>
<!-- post navigation -->
<?php else: ?>
<!-- no posts found -->
<?php endif; ?>

<?php 
// Restore original Post Data
wp_reset_postdata(); ?>
有什么想法吗??

如果你能指引我,我将不胜感激:)

3 个回复
最合适的回答,由SO网友:Mr.Happy 整理而成

尝试以下操作:

<?php
    $querydetails = "
        SELECT wposts.*             
          FROM $wpdb->posts wposts
        INNER
          JOIN ( SELECT post_id
                   FROM $wpdb->postmeta wpostmeta
                        WHERE ( 
                            ( wpostmeta.meta_key = \'type\' AND wpostmeta.meta_value LIKE \'%Collection1%\' )
                        OR 
                            ( wpostmeta.meta_key = \'type\'AND wpostmeta.meta_value LIKE \'%Collection2%\' )
                        OR 
                            ( wpostmeta.meta_key = \'type\'AND wpostmeta.meta_value LIKE \'%Collection3%\' )
                        OR
                            ( wpostmeta.meta_key = \'type\'AND wpostmeta.meta_value LIKE \'%Collection4%\' )
                        OR
                            ( wpostmeta.meta_key = \'type\'AND wpostmeta.meta_value LIKE \'%Collection5%\' )
                        )
                        GROUP BY post_id
                ) AS t
            ON t.post_id = wposts.ID
            WHERE wposts.post_status = \'publish\'
            AND wposts.post_type = \'book\'
            ORDER BY wposts.post_date DESC
         ";
    $pageposts = $wpdb->get_results($querydetails, OBJECT)
?>

<?php 
    if ($pageposts):
        foreach ($pageposts as $post):
            setup_postdata($post); 
?>
            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

<?php endforeach; endif; ?>
我希望这对你有用:)

SO网友:jjlm

<?php   
$args = array(


    //Type & Status Parameters
    \'post_type\'   => \'book\',
    \'post_status\' => \'publish\',

    //Order & Orderby Parameters
    \'order\'               => \'DESC\',
    \'orderby\'             => \'date\',

    //Pagination Parameters
    \'posts_per_page\'         => -1,
            // Here you can add you second meta value

    \'meta_query\'     => array(
                   // [\'relation\'] => \'OR\', // use this for a different comparison
        array(
            \'key\' => \'type\',
            \'value\' => \'Collection1\',
            \'compare\' => \'=\'
        ),
        array(
            \'key\' => \'type2\',
            \'value\' => \'Collection2\',
            \'compare\' => \'=\'
        )

);

$query = new WP_Query( $args );
?>

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>


<?php endwhile; ?>
<!-- post navigation -->
<?php else: ?>
<!-- no posts found -->
<?php endif; ?>

<?php 
// Restore original Post Data
wp_reset_postdata(); ?>
我将使用WP\\u查询和meta\\u查询,请参见https://codex.wordpress.org/Class_Reference/WP_Query 了解更多信息。

SO网友:Jonathan

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
        echo \'<ul>\';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo \'<li>\' . get_the_title() . \'</li>\';
    }
        echo \'</ul>\';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
插入包含参数的数组$args 你就完了。

结束

相关推荐

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

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