dtnpf35197 2017-04-18 02:06
浏览 38
已采纳

在post__not_in中使用外部数组排除Wordpress查询中的帖子

I am trying to pass an array via AJAX to an external php file. It's a hard-coded list of post IDs to be excluded from the query.

This is where I'm setting the post IDs in an array, these IDs exist in the database. Then I'm passing the variable $postsNotIn as an imploded array to the JS file to be passed over to ajax.php

The problem is, it is not working. I don't think the $postNotIn is being passed through as an array because post__not_in is expecting an array.

I tried passing $features instead but that didn't work either.

I keep getting 0 as a response.

However when I hardcode the array in the query 'post__not_in' => array(230, 280, 268); it works fine.

How can I pass the IDs in the correct array form to ajax.php?

EDIT if I wrap $exclude in array() like so 'post__not_in' => array($exclude) it only excludes the first post ID while ignoring the remaining 2.

the HTML button

$features = array(320, 280, 268); // Array of posts to exclude
$postsNotIn = implode(", ", $features);

<a class="btn btn-lg btn-default load-more-button" 
    data-exclude="<?php echo $postsNotIn; ?>" 
    data-page="1" 
    data-url="<?php echo admin_url('admin-ajax.php'); ?>">
    Load More
</a>

The Javascript

$('.load-more-button:not(.loading)').on('click', function() {
    var that = $(this);
    var ajaxurl = that.data('url');
    var page = that.data('page');
    var exclude = that.data('exclude');

    $.ajax({
            url: ajaxurl,
            type: 'post',
            data: {
                page: page,
                exclude: exclude,
                action: 'load_more'
            },
            error: function(response) {
                console.log(response);
            },
            success: function(response) {
                console.log(response);
            }

This is the external ajax.php file

function load_more() {
    $paged = $_POST['page'] + 1;
    $exclude = $_POST['exclude'];

    $query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'paged' => $paged,
        'post__not_in' => $exclude
    ));

if($query->have_posts()) :
    while($query->have_posts()) : $query->the_post();
        get_template_part( 'content', get_post_format() );
    endwhile;
else :
    echo 0;
endif;

wp_reset_postdata();

die();
  • 写回答

1条回答 默认 最新

  • dpxw7293 2017-04-18 02:24
    关注

    post__not_in is expecting an array of IDs whereas you're passing in a comma-delineated string.

    You need to reverse the process you ran when converting your array into a string for use in an attribute.

    // Before using the $exclude variable, convert the string to an array
    $exclude = array_map( 'trim', explode( ',', $exclude ) );
    

    Following our discussion in the comments section I've removed the space from the delimiter. The string being received by the AJAX callback didn't have any spaces.

    I'm using trim() on all elements in the array to account for any IDs which did have spaces.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器