• Resolved hassanjunaid

    (@hassanjunaid)


    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. I have also tried to create the posts with REST API, but facing the same issue. 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),
            'meta_input'    => array(
                'primary_category' => $category_id
            ),
            'page_template' => 'category.php'
                  
        );
        $post_id = wp_insert_post($post_data);
    
    
            }
        }

    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
Viewing 4 replies - 1 through 4 (of 4 total)
  • How is the pagination generated? From your theme? I think that would be interesting to know in order to answer your question.

    Thread Starter hassanjunaid

    (@hassanjunaid)

    Let me share the category.php code with you for better understanding.

    <?php 
        get_header();
    ?>
    <section>
        <div class="container">
            <div>
    	   <!-- Billboard Ad Start -->
                <?php get_template_part( 'template-parts/content/billboard-ad' ); ?>
                <!-- Billboard Ad End -->
            </div>
            <div class="row">
                <div class="col-md-12">
                    <!-- Breadcrumb -->
                    <ul class="breadcrumbs bg-light mb-4">
                        <?php echo website_breadcrumbs(); ?>
                    </ul>
                </div>
    
            </div>
        </div>
        <div class="container">
            <div class="row">
                <div class="col-md-8">
                    <aside class="wrapper__list__article ">
                        <h4 class="border_section">
                            <?php single_cat_title(); ?>
                        </h4>
                        <?php
                            $category_id = get_query_var('cat');
                            $args = array(
                                'cat' => $category_id,
                                'paged' => get_query_var( 'paged' )
                                
                                );
                            
                            $category_posts = new WP_Query($args);
                            if($category_posts->have_posts()){
                                while ($category_posts->have_posts()) {
                                    $category_posts->the_post();
                        ?>
                        <div class="card__post card__post-list card__post__transition mt-1">
                            <div class="row ">
                                <div class="<?php echo (is_category('opinion')?'col-md-3':'col-md-5');  ?> mb-2">
                                    <div class="card__post__transition">
                                        <a href="<?php the_permalink(); ?>">
                                            <?php if(is_category('opinion')){ ?>
                                                <img style="width: 150px;height: 150px;" src="<?php echo get_the_post_thumbnail_url(get_the_ID(),'large'); ?>" loading="lazy" class="img-fluid" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true );?>">
                                            <?php }else{ ?>
                                                <img src="<?php echo get_the_post_thumbnail_url(get_the_ID(),'large'); ?>" loading="lazy" class="img-fluid w-100" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true );?>">
                                            <?php } ?>
                                        </a>
                                    </div>
                                </div>
                                <div class="<?php echo (is_category('opinion')?'col-md-9':'col-md-7');  ?> my-auto pl-0">
                                    <div class="card__post__body ">
                                        <div class="card__post__content mb-1 ">   
                                            <span class="bg-primary p-1 text-white small ">
                                               <?php single_cat_title(); ?>
                                            </span>
                                            <div class="card__post__author-info mt-2 mb-2 ">
                                                <ul class="list-inline">
                                                    <li class="list-inline-item">
                                                        <span>
                                                            <a class="text-dark" href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>">
                                                            <?php the_author(); ?></a>
                                                        </span>
                                                        <span>
                                                              <a class="text-dark" href="<?php echo get_author_posts_url(get_field('co_author')); ?>">
                                                                <?php if(get_field('co_author')) echo get_the_author_meta('display_name', get_field('co_author')); else echo ""; ?></a>  
                                                        </span>
                                                    </li>
                                                    <li class="list-inline-item">
                                                        <span class="text-dark text-capitalize">
                                                            <?php  echo time_display(); ?>
                                                        </span>
                                                    </li>
                                                </ul>
                                                <?php if (is_category('opinion')) { ?>
                                                    <div class="text-dark mt-2">
                                                        <?php echo get_field('article_author'); ?>
                                                    </div>
                                                <?php } ?>
                                            </div>
                                            <div class="card__post__title">
                                                <h5>
                                                    <a style="text-transform: none;" class="truncate" href="<?php the_permalink(); ?>" title="<?php echo get_the_title(); ?>">
                                                        <?php the_title(); ?>
                                                    </a>
                                                </h5>
                                                <p class="d-none d-lg-block d-xl-block mb-0">
                                                    <?php  
                                                        if(has_excerpt()){
                                                            the_excerpt();
                                                        }
                                                        else{
                                                            echo "";
                                                        }
                                                    ?>
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php
                                }
                            }
                            wp_reset_postdata();
    
                        ?>
                    </aside>
                </div>
                     <!-- Sidebar Start -->
                    <?php (is_category('latest'))? get_template_part( 'template-parts/content/sidebar-latest' ) : get_template_part( 'template-parts/content/sidebar' ) ; ?>
                    <!-- Sidebar End -->
            </div>
            <!-- Pagination Start -->
            <div class="mx-auto">            
                <div class="pagination-area">
                    <h6>
                        <?php
                            $args = array (
                                'prev_text' => '?',
                                'next_text' => '?'
                            );
                            echo paginate_links( $args );
                        ?>     
                    </h6>
                </div>
            </div>  
            <!-- Pagination End -->
        </div>
    </section>
    
    
    
    <?php
    get_footer();
     ?>

    This code is working perfectly fine for all the categories, also the posts with “videos” category, added previously with editor. The issue is only when I create posts through code.

    • This reply was modified 6 months, 2 weeks ago by hassanjunaid.

    The “page_template” specification does not work for you at all. According to the manual, it is only used if the post type is “page”. Leave the specification out.

    If I understand correctly, do you want to display the posts in the Videos category with their own template? If I remember correctly, you have to name the template accordingly as video-category.php, not just category.php, as this would be the default view of the categories.

    Thread Starter hassanjunaid

    (@hassanjunaid)

    Thank you for your time. The issue which actually never existed has been resolved. I was using the wrong URL. Again thanks a bundle.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘404 error on all other pages except page one in WordPress pagination on category’ is closed to new replies.