Get post list in same category

Creating a system to load the next post within the same category can indeed be achieved with modifications to your existing code. Here’s how I think you should modify it so it works:

// Assuming you have the current post ID stored in $current_post_id
$current_post_id = get_the_ID(); // Retrieve the current post ID
$current_post_categories = wp_get_post_categories($current_post_id);
$listed_posts = isset($_POST["listed_posts"]) && !empty($_POST["listed_posts"]) && is_array($_POST["listed_posts"]) ? $this->isrp_ll_sanitize_post_ids($_POST["listed_posts"]) : array();

$next_post = null;

if (!empty($current_post_categories)) {
    // Get the first category slug (assuming there's only one category per post)
    $category_id = $current_post_categories[0];

    $args = array(
        'category' => $category_id,
        'orderby' => 'date',
        'order' => 'DESC',
        'exclude' => $listed_posts,
        'post_status' => 'publish'
    );
    $next_post = get_posts($args);
}

// If no next post within the category, get a random post.
if (!$next_post) {
    $args = array(
        'orderby' => 'rand',
        'exclude' => $listed_posts,
        'post_status' => 'publish'
    );
    $next_post = get_posts($args);
}

$response = false;
if (!empty($next_post)) {
    $next_post = $next_post[0]; // get_posts returns array, get first post from it
    $response = new stdClass;
    $response->post_id = $next_post->ID;
    $response->permalink = get_permalink($response->post_id);
}

echo json_encode($response);
die("");

This code first attempts to fetch a next post from the same category. If none is found (or the current post has no category), it retrieves a random post. Note that this assumes that your posts could belong to multiple categories but you’re interested only in the first one.

Please replace ‘date’ and ‘DESC’ in the ‘orderby’ and ‘order’ arguments if you need a different sorting criterion.

And of course, I haven’t totally tested this code. So feel free to do tweaks to it as necessary.

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino