最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

get posts - Array merging multiple get_posts throwing Undefined offset error

programmeradmin2浏览0评论

Good morning. I've been trying to create a related posts function. The broad idea being; the user can manually select related posts using an ACF relationship drag and drop interface, but if they don't select enough (or don't select any), the function then grabs posts of the same category and, finally, if the quota still isn't reached it will just make the quota by getting posts of the same type.

The code is below, you'll see it uses get_posts and array_merge to work it's way through the 3 step process.

$current_id = $post->ID;
    $excluded = array( $current_id );
    $total_required = 12;
    $obtained = 0;
    $related = array();

    // Check to see if any posts have been selected via ACF relationship
    $selected = get_field('project_related');
    if( $selected ) {
        $obtained = count( $selected );
        if ( $obtained < $total_required ) {
            $selected_ids = get_field('project_related', false, false); // Returns an array of IDs
            $excluded = array_merge($selected_ids, $excluded);
        }
        $related = $selected;
    }

    // If we do not have enough find posts with matching categories
    if ( $obtained < $total_required ) {
        $required = $total_required - $obtained;
        $post_type = get_post_type($current_id);
        if ( ! empty( $categories ) ) {
            $category_ids = wp_list_pluck( $categories, 'cat_ID' ); // To return IDs only
            $args = array(
                'posts_per_page' => $required,
                'cat' => $category_ids,
                'post_type' => $post_type,
                'exclude' => $excluded
            );
            $same_category = get_posts( $args );
            if( $same_category ) {
                $obtained = $obtained + count( $same_category );
                $same_category_ids = wp_list_pluck( $same_category, 'ID' );
                $excluded = array_merge($same_category_ids, $excluded);
                $related = array_merge($same_category, $related);
            }
        }
    }

    // As a last resort just get more of the same post_type
    if ( $obtained < $total_required ) {
        $required = $total_required - $obtained;
        $args = array(
            'posts_per_page' => $required,
            'post_type' => $post_type,
            'exclude' => $excluded
        );
        $posts = get_posts( $args );
        if( $posts ) {
            $related = array_merge($posts, $related);
        }
    }

I do get posts back but I'm given the following error:

Notice: Undefined offset: 0 in K:\local\x\wp\wp-includes\class-wp-query.php on line 3323

I suspect this is something to do with the arrays not matching up but I'm not entirely sure and would really appreciate any help you experts can give me.

This is what I'm using to output at the moment:

foreach ( $related as $post ) :
    setup_postdata( $post );
    include get_template_directory() . '/includes/thumbnail.php';
endforeach;
wp_reset_postdata();

The include simply contains the_title();.

发布评论

评论列表(0)

  1. 暂无评论