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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度