使查询更简单,只需一次查询

时间:2017-01-18 作者:Track

如何在一个查询中使此查询更简单?

问题-只有主题才有meta\\u键“include\\u newsletter\\u feed”,因此我需要获取所有帖子(“产品”、“帖子”、“页面”),不包括类别(124、52)
并使用meta连接所有主题。

丑陋代码示例

$arg =  array(
        "post_type" => array("product", "post", "page"),
        "posts_per_page" => 3,
        \'category__not_in\' => array( 124 /*Gallery*/, 52 /* Discounts */ )
    );
$_posts = get_posts($arg);
$arg =  array(
        "post_type" => "topic",
        "posts_per_page" => $nimbus_posts_on_home,
        \'meta_key\' => \'include_newsletter_feed\',
        \'meta_value\' => \'true\',
    );
$_topicts = get_posts($arg);
$_all_posts = array_merge ($_topicts, $_posts);
$ids = array();
foreach($_all_posts as $_post) {
        $ids[] = $_post->ID;
    }
/* Final query */
$wp_query = new WP_Query(array( "post_type" => array("product", "post", "page", "topic"), "post__in" => $ids ));

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

现在它只适用于过滤器“posts\\u groupby”、“posts\\u where”和“posts\\u join”<如果您知道更好的解决方案,请写信。

$args =  array(
    "post_type" => array("product", "post", "page", "topic"),
    "posts_per_page" => $nimbus_posts_on_home,
    \'category__not_in\' => array( 124 /*Gallery*/, 52 /* Discounts */ ),
);
add_filter( \'posts_groupby\', \'recent_posts_widget_groupby\' );
add_filter( \'posts_where\', \'recent_posts_widget_where\', 10, 2 );
add_filter( \'posts_join\', \'recent_posts_widget_join\', 10, 1 );  

function recent_posts_widget_groupby( $groupby ) {
    remove_filter( \'posts_groupby\', \'recent_posts_widget_groupby\' );
    if( strpos($groupby, \'wp_posts.ID\' ) === false )
        $groupby .= " wp_posts.ID ";
    return $groupby;
}

function recent_posts_widget_join( $join ){
    remove_filter( \'posts_join\', \'recent_posts_widget_join\' );
    $join .= " INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) 
     INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) ";
    return $join;
}

function recent_posts_widget_where( $where, &$wp_query ) {
    remove_filter( \'posts_where\', \'recent_posts_widget_where\' );
    $post_types = array();
    foreach( $wp_query->query[\'post_type\'] as $post_type) {
        if( $post_type != \'topic\')
            $post_types[] = $post_type;
    }
    $post_type_in = implode("\',\'", $post_types );

    $where = " AND( 
      wp_posts.post_type IN ( \'$post_type_in\' )
      OR 
      ( 
        wp_posts.post_type=\'topic\' AND( mt1.meta_key = \'include_newsletter_feed\' AND mt1.meta_value = \'true\' )
      )
    ) ". $where;
    return $where;
}
$wp_query = new WP_Query($args);
最后的SQL查询如下所示

SELECT * FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) 

 INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )  WHERE 1=1  AND( 

  wp_posts.post_type IN ( \'post\',\'page\',\'product\' )
  OR 
  ( 
    wp_posts.post_type=\'topic\' AND( mt1.meta_key = \'include_newsletter_feed\' AND mt1.meta_value = \'true\' )
  )

)  AND ( 
wp_posts.ID NOT IN (
            SELECT object_id
            FROM wp_term_relationships
            WHERE term_taxonomy_id IN (164,142)
        )
) AND wp_posts.post_type IN (\'post\', \'page\', \'topic\', \'product\') AND ((wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'closed\')) 
GROUP BY wp_posts.ID  ORDER BY wp_posts.post_date DESC LIMIT 0, 3

SO网友:codiiv

You can try this

<?php 

    $args=  array(
            "post_type" => array("product", "post", "page"), 
             "cat" => \'-124,-52\', 
             "meta_query" => array(
               array(
                "key"     => \'include_newsletter_feed\',
                \'value\'   => "",
                \'compare\' => \'!=\'
                   ),
              ),

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

相关推荐

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

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