{"id":89,"date":"2011-01-29T17:07:28","date_gmt":"2011-01-29T16:07:28","guid":{"rendered":"http:\/\/wpseek.com\/blog\/?p=89"},"modified":"2022-03-07T09:40:20","modified_gmt":"2022-03-07T08:40:20","slug":"custom-post-type-pagination","status":"publish","type":"post","link":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/","title":{"rendered":"Custom Post Type Pagination"},"content":{"rendered":"<h4>The problem<\/h4>\n<p>I was just messing around with a custom post type loop,\u00a0 and noticed that pagination wasn&#8217;t showing up at all. Neither <a title=\"posts_nav_link on wpseek.com\" href=\"http:\/\/wpseek.com\/posts_nav_link\/\" target=\"_blank\" rel=\"noopener\">posts_nav_link()<\/a> nor the <a title=\"WP-PageNavi\" href=\"http:\/\/wordpress.org\/extend\/plugins\/wp-pagenavi\/\" target=\"_blank\" rel=\"noopener\">WP-PageNavi<\/a> plugin was working and showing up navigation links.<!--more--><\/p>\n<p>I was basically using a custom WP_Query like this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">$custom_query = new WP_Query( $args );\r\nwhile ($custom_query-&gt;have_posts()) :\r\n...<\/pre>\n<h4>The solution<\/h4>\n<pre class=\"brush: php; highlight: [5]; title: ; notranslate\" title=\"\">global $wp_query;\r\n$clone_page_total = $wp_query-&gt;max_num_pages;\r\n$wp_query-&gt;max_num_pages = $custom_query-&gt;max_num_pages;\r\n\r\nwp_pagenavi();\r\n\r\n$wp_query-&gt;max_num_pages = $clone_page_total;\r\n<\/pre>\n<p>Just make sure to put your pagination function call (<em>wp_pagenavi()<\/em> in this case) before we restore the $wp_query property to it&#8217;s original value (in line 7).<\/p>\n<h4>What does it do?<\/h4>\n<p>The problem is that WordPress pagination functions need the <em>$wp_query<\/em> object. They don&#8217;t care for a custom <em>$custom_query<\/em> object. We basically just pretend there is a <em>$wp_query<\/em> object by cloning the needed property <em>max_num_pages<\/em> to <em>$wp_query<\/em>.<\/p>\n<h4>Requirements<\/h4>\n<ul>\n<li>WordPress 3.0 (if you use custom post types)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The problem I was just messing around with a custom post type loop,\u00a0 and noticed that pagination wasn&#8217;t showing up at all. Neither posts_nav_link() nor the WP-PageNavi plugin was working and showing up navigation links.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[21,33,31,34,35,32],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-wordpress-hacks","tag-custom","tag-custom-post-type","tag-pagination","tag-post","tag-wp_query","tag-wp-pagenavi"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Custom Post Type Pagination &#8211; wpseek.com \/\/ blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom Post Type Pagination &#8211; wpseek.com \/\/ blog\" \/>\n<meta property=\"og:description\" content=\"The problem I was just messing around with a custom post type loop,\u00a0 and noticed that pagination wasn&#8217;t showing up at all. Neither posts_nav_link() nor the WP-PageNavi plugin was working and showing up navigation links.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/\" \/>\n<meta property=\"og:site_name\" content=\"wpseek.com \/\/ blog\" \/>\n<meta property=\"article:published_time\" content=\"2011-01-29T16:07:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-07T08:40:20+00:00\" \/>\n<meta name=\"author\" content=\"wpseek (admin)\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"wpseek (admin)\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2011\\\/custom-post-type-pagination\\\/89\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2011\\\/custom-post-type-pagination\\\/89\\\/\"},\"author\":{\"name\":\"wpseek (admin)\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\"},\"headline\":\"Custom Post Type Pagination\",\"datePublished\":\"2011-01-29T16:07:28+00:00\",\"dateModified\":\"2022-03-07T08:40:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2011\\\/custom-post-type-pagination\\\/89\\\/\"},\"wordCount\":189,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\"},\"keywords\":[\"custom\",\"Custom Post Type\",\"Pagination\",\"Post\",\"WP_Query\",\"WP-PageNavi\"],\"articleSection\":[\"WordPress Hacks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/wpseek.com\\\/blog\\\/2011\\\/custom-post-type-pagination\\\/89\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2011\\\/custom-post-type-pagination\\\/89\\\/\",\"url\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2011\\\/custom-post-type-pagination\\\/89\\\/\",\"name\":\"Custom Post Type Pagination &#8211; wpseek.com \\\/\\\/ blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#website\"},\"datePublished\":\"2011-01-29T16:07:28+00:00\",\"dateModified\":\"2022-03-07T08:40:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2011\\\/custom-post-type-pagination\\\/89\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wpseek.com\\\/blog\\\/2011\\\/custom-post-type-pagination\\\/89\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2011\\\/custom-post-type-pagination\\\/89\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Custom Post Type Pagination\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/\",\"name\":\"wpseek.com \\\/\\\/ blog\",\"description\":\"A random blog about WordPress\",\"publisher\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\",\"name\":\"wpseek (admin)\",\"logo\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"http:\\\/\\\/wpseek.com\"],\"url\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/author\\\/wpseek\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Custom Post Type Pagination &#8211; wpseek.com \/\/ blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/","og_locale":"en_US","og_type":"article","og_title":"Custom Post Type Pagination &#8211; wpseek.com \/\/ blog","og_description":"The problem I was just messing around with a custom post type loop,\u00a0 and noticed that pagination wasn&#8217;t showing up at all. Neither posts_nav_link() nor the WP-PageNavi plugin was working and showing up navigation links.","og_url":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/","og_site_name":"wpseek.com \/\/ blog","article_published_time":"2011-01-29T16:07:28+00:00","article_modified_time":"2022-03-07T08:40:20+00:00","author":"wpseek (admin)","twitter_card":"summary_large_image","twitter_misc":{"Written by":"wpseek (admin)","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/#article","isPartOf":{"@id":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/"},"author":{"name":"wpseek (admin)","@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb"},"headline":"Custom Post Type Pagination","datePublished":"2011-01-29T16:07:28+00:00","dateModified":"2022-03-07T08:40:20+00:00","mainEntityOfPage":{"@id":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/"},"wordCount":189,"commentCount":0,"publisher":{"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb"},"keywords":["custom","Custom Post Type","Pagination","Post","WP_Query","WP-PageNavi"],"articleSection":["WordPress Hacks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/","url":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/","name":"Custom Post Type Pagination &#8211; wpseek.com \/\/ blog","isPartOf":{"@id":"https:\/\/wpseek.com\/blog\/#website"},"datePublished":"2011-01-29T16:07:28+00:00","dateModified":"2022-03-07T08:40:20+00:00","breadcrumb":{"@id":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpseek.com\/blog\/2011\/custom-post-type-pagination\/89\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpseek.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Custom Post Type Pagination"}]},{"@type":"WebSite","@id":"https:\/\/wpseek.com\/blog\/#website","url":"https:\/\/wpseek.com\/blog\/","name":"wpseek.com \/\/ blog","description":"A random blog about WordPress","publisher":{"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpseek.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb","name":"wpseek (admin)","logo":{"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/wpseek.com"],"url":"https:\/\/wpseek.com\/blog\/author\/wpseek\/"}]}},"_links":{"self":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":2,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":12770,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts\/89\/revisions\/12770"}],"wp:attachment":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}