WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination with custom post type listing (45 posts)

  1. Vayu Robins
    Member
    Posted 3 years ago #

    Nice work @rafaelxy!

    What is it, in this global variable "$wp_query", that fixes this issue?

    <?php
    global $wp_query;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
    	'post_type' => 'CUSTOM_POST_NAME',
    	'product_category' => 'CATEGORY_NAME',
    	'posts_per_page' => 6,
    	'post_status' => 'publish',
    	'orderby' => 'menu_order',
    	'order' => 'DESC',
    	'paged' => $paged
    );
    $wp_query = new WP_Query($args);
    
    while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    	<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>

    I am just trying to understand this, that's all. :-)

    Thanks
    Vayu

  2. jbiddulph
    Member
    Posted 3 years ago #

    hi,

    I am having this problem, I have installed the plugin fix and that has fixed the 404 error although when I try to page it goes to the main blog page (News - which is not a custom post type)

    My custom Post Type is named 'Work Items'

    I have tried adding this code to my page but it didn't work:
    [Code moderated as per the Forum Rules. Please use the pastebin]

  3. rafaelxy
    Member
    Posted 3 years ago #

    I'm back on this thread after months, I'm glad a lot of people managed to find it and get helped :D

    @Vayu the global variable,

    global $wp_query

    changes according to the URL that your HTTP request is requiring. If you are going to custom modify its behaviour inside a page (eg: listing a custom post type), you NEED to reset it using

    wp_reset_query();

    so no parameters from the HTTP request get in the way of your custom query.

  4. rafaelxy
    Member
    Posted 3 years ago #

    @jbiddulph

    put this code in your functions.php
    http://rafaelxy.pastebin.com/VtHQEkH7

    and use this custom_query_posts() function instead of "new WP_Query()" or "query_posts()"

    just remember to pass the params as an array, and not as a query string.

  5. Vayu Robins
    Member
    Posted 3 years ago #

    Thanks rafaelxy!

    I appreciate the explanation and tip. :-)

  6. cpkid2
    Member
    Posted 3 years ago #

    Thanks for the tip on having the page name and post type name different.

    I just have one question: If I name the post type something like "my-projects" and name the page "projects", the link points to mysite.com/my-projects/ instead of mysite.com/projects

    Anyway I can fix this?

  7. merlinox
    Member
    Posted 3 years ago #

    I'm trying without success all solution, also @rafaelxy solution.
    Pagination links don't appear.

    Sorry but custom_query_posts must add in function or is good in template page too?

    Thanks

  8. merlinox
    Member
    Posted 3 years ago #

    Sorry: now it works. I put function on template. First I wronged args array. Little question: why function and not plain code?

    Thanks.

  9. bincyclt
    Member
    Posted 3 years ago #

    Great..!!!

    Thanks Colleagues...

    Thank you Very Much.. :-)

  10. lancemonotone
    Member
    Posted 3 years ago #

    I found the answer here.

    Basically, you need to make sure the 'Blog pages show at most' item in Settings->Reading is low enough so there are at least that many posts on the final page of your custom page loop. For instance, if your setting is 15 and there are only 5 posts on the final page of your loop, you'll get a 404.

    If there's a way to override this in your custom query, please let me know.

    Note: Oddly, if you're logged in as an admin the pagination seems to work correctly.

  11. Bowe
    Member
    Posted 3 years ago #

    @Lancemonotone: THANKS!! It was the freaking Settings > Reading issue which caused the 404 page. What the... I finally got everything working. Pff

  12. happygentel
    Member
    Posted 3 years ago #

    @rafaelxy DUDE THANKS A LOT!! KEEP IT UP! ^^

  13. enzolarosa
    Member
    Posted 3 years ago #

    i'm trying without succes.. :( i use this code

    [Code moderated as per the Forum Rules. Please use the pastebin]

    but don't work.. :(

  14. enzolarosa
    Member
    Posted 3 years ago #

    i have past the code on http://pastebin.com/sPvVgseA

  15. unexploded
    Member
    Posted 3 years ago #

    I Just want to thank TakeoK, your code works for me, thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic