Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从get_posts()中排除帖子_Php_Wordpress - Fatal编程技术网

Php 从get_posts()中排除帖子

Php 从get_posts()中排除帖子,php,wordpress,Php,Wordpress,早上好,我发现了许多类似的问题,但没有一个答案适合我的问题。要点很简单:我有一个带有get_posts()的自定义循环,我想将当前帖子从显示中排除 代码是: $args = array( 'posts_per_page' => 3, 'orderby' => 'meta_value', 'order' => 'ASC', 'post_type'

早上好,我发现了许多类似的问题,但没有一个答案适合我的问题。要点很简单:我有一个带有get_posts()的自定义循环,我想将当前帖子从显示中排除

代码是:

$args = array(
          'posts_per_page'    => 3,
          'orderby'           => 'meta_value',
          'order'             => 'ASC',
          'post_type'         => 'fasthomepress_pt',
          'post__not_in'      => array(get_the_id()),
          'meta_query'        => array(
                                    array(
                                        'key' => 'custom_richiesta',
                                        'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
                                        'type' => 'numeric',
                                        'compare' => 'BETWEEN'
                                      )
                              )
      );
我试过:

'post__not_in' => array(get_the_ID),
'post__not_in' => array($post->ID),
'exclude'      => $post->ID,
'exclude'      => get_the_ID,
和许多其他组合,有或没有数组。当然,当前的post id在这个循环之前被正确地回显,如果我尝试回显($post->id)和回显(get_the_id()),我会得到相同的、正确的结果

我真的不知道发生了什么, 非常感谢你的帮助


马可

尝试
排除

$args = array(
      'posts_per_page'    => 3,
      'orderby'           => 'meta_value',
      'order'             => 'ASC',
      'post_type'         => 'fasthomepress_pt',
      'exclude'      => array(get_the_id()),
      'meta_query'        => array(
                                array(
                                    'key' => 'custom_richiesta',
                                    'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
                                    'type' => 'numeric',
                                    'compare' => 'BETWEEN'
                                  )
                          )
  );

下面是一个函数,它的作用就是:

    function get_lastest_post_of_category($cat){
    $args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
    $post_is = get_posts( $args );
    return $post_is[0]->ID;
}
用法:假设我的类别id为22,则:

$last_post_ID = get_lastest_post_of_category(22);
还可以将类别数组传递给此函数

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    'posts_per_page'   => 18,
     'paged'           => $paged,
    'offset'           => 0,
    'post__not_in'     => array($last_post_ID,),
    'category'         => '',
    'category_name'    => '',
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true
);
// The Query
$the_query = new WP_Query( $args );

现在尝试删除meta_查询以进行测试,并检查它是否能排除当前帖子。语法是正确的,我想可能是元查询的问题。我现在解决了,这是我的错误!!我在另一个循环中尝试了这个get_posts()循环,但没有很好地放置它。我把这个循环移到主循环之外,一切都很好,谢谢你的支持!谢谢你的回答。不幸的是,它不起作用,我尝试使用exclude-->array()和exclude-->value,结果总是一样的。解决了!这是我的错误。我在另一个循环中尝试了这个get_posts()循环,但没有很好地放置它。我把这个循环移到主循环之外,一切都很好,谢谢你的支持!谢谢分享!我马上就去试试!