0

I am currently working on an eCommerce project where I am coding a non-WooCommerce dependent theme for eCommerce. The current problem with me is, I am coding archive-product.php which is a custom post type archive. Everything went well, I coded the archive-product.php as search page for the eCommerce and everything is working absolutely fine. BUT, when I try to add 'load more posts link', it does not show up. Wait, let me show you:

<?php get_header(); ?>
    <main id="products" class="products" role="main">
        <?php $s = get_search_query(); $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 's' =>$s, 'post_type' => 'product', 'posts_per_page' => 2, 'paged' => $paged ); $the_query = new WP_Query( $args ); ?>
        <?php if ( $the_query->have_posts() ) : ?>
        <h1 class="searchResult"><span><span id="searchTotalResult"><?php global $wp_query; echo $wp_query->found_posts.''; ?></span> <span>Search Results for</span> <span id="searchTerms">"<?php echo get_search_query(); ?>"</span></span></h1>
        <ul class="productList">
            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <li class="productItem">
                <a class="productURL" href="<?php the_permalink(); ?>">
                    <img class="lazyLoad" data-src="<?php the_post_thumbnail_url(); ?>" />
                    <h2 class="productTitle"><?php the_title(); ?></h2>
                    <span class="productPrice">$<?php echo esc_html( get_post_meta( get_the_ID(), 'price', true ) ); ?>.00</span>
                </a>
            </li>
            <?php endwhile; ?>
        </ul>
        <?php next_posts_link('Older &raquo;') ?>
        <?php wp_reset_postdata(); ?>
        <?php else:  ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; ?>
    </main>
<?php get_footer(); ?>

As far as I know, everything is looking good to me. But as I assigned "next_posts_link('')", it is not showing up in the site's front-end. it seems like it is loading literally nothing.

enter image description here

Can you please help me out get load more products link for the archive page? I don't know some other way to enqueue paged link in the archive page. And, if you think I did not add 'has_archive' ==> true while registering the post type- I did.

Please help out.

0

You shouldn't be using new WP_Query() in archive-product.php (or any archive template). The right posts have already been queried, and this is what the pagination is based on. You just need to loop through the right posts with the standard loop. Look at the default archive template in WooCommerce:

while ( have_posts() ) {
        the_post();

        // etc.
    }
}

Notice that there's no $the_query or anything like that.

6
  • Thanks bud! But if I use the default loop system as you advised, should it make $args I have set to stop working? Because I am using this archive template as search results page. Feb 19 at 7:13
  • How do I make sure that the args work with default loop query as well? Feb 19 at 7:14
  • You shouldn't need to se $args. As I said, WordPress will query the right posts automatically, including any search terms. Feb 19 at 7:14
  • If you want to change something like the number of posts/products per page, then there's other ways to do that, and that would be a separate question. However this is thoroughly documented elsewhere, so I suggest searching first. Feb 19 at 7:15
  • Bud, I have tried using a deafult loop and the result was, I was not able to get search results without adding these args Feb 19 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.