skip to Main Content

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


  1. Chosen as BEST ANSWER

    I have already done the following:

    1. Updating the permalink settings
    2. Updating the .htacess file
    3. Made changes to the $wp_query using pre_get_posts
    function modify_main_query($query) {
        if ( ! is_admin() && $query->is_main_query() && ( $query->is_category() || $query->is_post_type_archive() ) ) {
            // Adjust the query parameters
            $query->set('post_type', array('post'));  
            $query->set('posts_per_page', 10);    }
    }
    add_action('pre_get_posts', 'modify_main_query');
    
    1. The posts_per_page value is same as the Settings/Reading argument.

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search