Im trying to insert a post type called advert after every fourth post, I am able to list the 1st post but it dosent page with the main loop. Any help would be appreciated. Here is the code so far.

function load_next_projects() {
global $post;

if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'forum_ajax' ) ) {
    wp_send_json_error();
}

$pageID = $_REQUEST['pageid'];

//set the query args
$args = array( 'post_type' => 'post' );

if ( isset( $_REQUEST['posts_number'] ) && 'all' == $_REQUEST['posts_number'] ) {
    $args['posts_per_page'] = 999;
} else {
    $args['posts_per_page'] = intval( get_post_meta( $pageID, '_forum_posts_per_page', true ) );
}


if ( isset( $_REQUEST['taxonomy'] ) ) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => $_REQUEST['taxonomy'],
            'field'    => 'term_id',
            'terms'    => array( $_REQUEST['term_id'] ),
        ),
    );
}

//check if we have a offset in $_REQUEST
if ( isset( $_REQUEST['offset'] ) ) {
    $args['offset'] = (int) $_REQUEST['offset'];
}

$posts = get_posts( $args );

if ( ! empty( $posts ) ) {
    ob_start();

    // initialize the item counter
    $forum_item_index = 0;
    foreach ( $posts as $post ) : setup_postdata( $post );
        //increase the counter beforehand since we start at 0

    $forum_post_index++;

    forum_wrap_content_before( $forum_post_index, $post->ID );

        get_template_part( 'template-parts/content-portfolio' );

        orum_wrap_content_after( $forum_post_index );

    endforeach;

    /* Restore original Post Data */
    wp_reset_postdata();

    wp_send_json_success( array(
        'posts' => ob_get_clean(),
    ) );
} else {
    wp_send_json_error();
}

}

Read more here: inserting a second loop into a infinite scroll loop


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: