I am fetching YouTube feed from api in WordPress and creating posts from that data, assigning it "videos" category. I am facing a problem in pagination in category pages only for this category page when, basically it creates the pagination links if more posts are created but giving 404 error on all the other pages except first one. I am not using custom post type but the default "post" type to create posts through my code. When I remove, all the created file, the pagination on this category works fine. Thanks in advance for help.
$data = file_get_contents($url);
if(!empty($data)){
$data = json_decode($data);
foreach ($data->items as $item) {
$title = $item->snippet->title;
$description = $item->snippet->description;
$video = $item->id->videoId;
$thumbnail = $item->snippet->thumbnails->default->url;
$content = '<p style="text-align:center">https://www.youtube.com/watch?v='.$video.'</p>';
// Prepare post data
$post_data = array(
'ID' => 0,
'post_author' => $user->ID,
'post_content' => $content,
'post_content_filtered' => '',
'post_title' => $title,
'post_excerpt' => $description,
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => sanitize_title($title),
'to_ping' => '',
'pinged' => '',
'post_parent' => 0,
'menu_order' => 0,
'post_mime_type' => '',
'guid' => '',
'import_id' => 0,
'post_category' => array( $category_id), // Set the category IDs
'tags_input' => '',
'tax_input' => array( $category_id),
'meta_input' => array(
'primary_category' => 'videos'
),
'page_template' => 'category.php'
);
// Insert the post into the database
$post_id = wp_insert_post($post_data);
}
}
2
Answers
I have already done the following:
Update permalink settings
Go to Settings > Permalinks in your WordPress admin, and click Save Changes. This will update the permalink settings and flush the rewrite rules.
Update the .htaccess file
If updating the permalink settings doesn’t work, you can try manually updating the .htaccess file.
Change the $wp_query
Make changes to the $wp_query using pre_get_posts.
Check the posts_per_page value
One user reported that setting the posts_per_page value to the same value as the Settings/Reading argument resolved the issue