简体   繁体   English

如何在Wp_query中包含一个帖子?

[英]How can I include a single post inside a Wp_query?

My current query looks like this. 我当前的查询如下所示。

$tag_id = 276;
$tag = get_tag($tag_id);
$today = date( "Y-m-d" );

$args  = array(
    'post_type'     => array(
                        'post',
                        'review',
                        'cardguide'
                    ),
    'taxonomy'      => 'post_tag',
    'tag_id'        => $tag_id,
    'posts_per_page'=> - 1,
    'orderby'       => 'id',
    'order'         => 'DESC',

    'meta_key'      => 'event_end_date',
    'meta_query'    => array(
        'relation'=> 'AND',
        array(
            'key'    => 'event_start_date',
            'value'  => $today,
            'compare'=> '<=',
            'type'   => 'DATE'
        ),
        array(
            'key'    => 'event_end_date',
            'value'  => $today,
            'compare'=> '>=',
            'type'   => 'DATE'
        )
    )

);
$my_query = new Wp_query( $args );

I want to include a single post (can use post id) into this query. 我想在此查询中包含一个帖子(可以使用帖子ID) The post may not obey each of the rules I defined inside $args . 该帖子可能不遵守我在$args定义的每个规则。 And also would like to have it always on top of the list (featured item) . 并且还希望它始终位于列表的顶部(功能部件)

If you are looking to get the same post by id for this query every time, then just do a separate get_post() call like: 如果您希望每次通过此查询通过id获取相同的帖子,则只需执行一个单独的get_post()调用,例如:

$featured_item = get_post( 123 ); // replace 123 with your id

This will prevent you from having to worry about "obeying each of the rules" you've defined above. 这将使您不必担心上面已定义的“遵守每个规则”。 Plus, if you always want it "on top", then you can pump out the values of $featured_item before you run your query loop. 另外,如果您始终希望它位于“顶部”,则可以在运行查询循环之前抽出$ featured_item的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM