[wp-trac] [WordPress Trac] #18562: WP_Query class ignores all arguments in custom post type

WordPress Trac wp-trac at lists.automattic.com
Thu Sep 1 04:35:31 UTC 2011


#18562: WP_Query class ignores all arguments in custom post type
-------------------------------+------------------------------
 Reporter:  arsalkeen          |       Owner:
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  General            |     Version:  3.2.1
 Severity:  critical           |  Resolution:
 Keywords:  reporter-feedback  |
-------------------------------+------------------------------

Comment (by arsalkeen):

 Here goes the code...

 I include a file with the following code in my functions.php to register
 the new post type.
 {{{
 //Adding custom post type eb_slider
 add_action( 'init', 'create_service_post_type' );
 function create_service_post_type() {

         $labels = array(
             'name' => _x('Services', 'post type general name'),
             'singular_name' => _x('Service', 'post type singular name'),
             'add_new' => _x('Add New', 'slider'),
             'add_new_item' => __('Add New Service'),
             'edit_item' => __('Edit Service'),
             'new_item' => __('New Service'),
             'all_items' => __('All Services'),
             'view_item' => __('View Service'),
             'search_items' => __('Search Service'),
             'not_found' =>  __('No services found'),
             'not_found_in_trash' => __('No services found in Trash'),
             'parent_item_colon' => '',
             'menu_name' => 'Services'

         );

         register_post_type( 'eb_service',
                                                 array(
                                                                 'labels'
 => $labels,
 'description'   => __( 'This is the post type to display services.' ),
                                                                 'public'
 => true,
                                                                 'supports'
 => array('title', 'editor', 'thumbnail', 'author', 'custom-fields',
 'excerpt'),
                                                                 'show_ui'
 => true,
 'menu_position' => 6,
 'capability_type'       => 'post',
 'has_archive'   => 'service',
                                                                 'rewrite'
 => array('slug' => 'service', 'with_front' => false, 'pages' => true),
 'query_var'             => true,
 'hierarchical'  => true
                                                 )
         );
 }
 }}}

 next is my archive-eb_service.php
 {{{
 <?php
 /* Template Name: Service Archive Template */

 get_header();

 global $show_title;

 ?>

 <div id="content_header">
         <div id="content_header_grid" class="container_12">
                 <div id="demo_reel" class="grid_12">
                         <?php if (function_exists('dimox_breadcrumbs'))
 dimox_breadcrumbs(); ?>
                         <h1 class="pageTitle"><?php wp_title('', true,
 'right'); ?></h1>
                         <?php $show_title = false; ?>
                 </div><!-- #demo_reel -->
                 <div class="clear"></div><!-- .clear -->
         </div><!-- #content_header_grid -->
 </div><!-- #content_header -->

 <div id="content">
         <div id="content_grid" class="container_12">
                 <div id="content_main" class="grid_9">
                         <div id="sidebar_shadow_top"
 class="push_8"></div><!-- #sidebar_shadow_top -->
                         <div id="content_middle" class="grid_9 alpha
 omega">
                                 <?php get_template_part('loop',
 'service_archive'); ?>
                         </div><!-- #content_middle -->
                         <div class="clear"></div><!-- .clear -->
                         <div id="sidebar_shadow_bottom"
 class="push_8"></div><!-- #sidebar_shadow_bottom -->
                 </div><!-- #content_main -->

                 <?php get_sidebar(); ?>

                 <div class="clear"></div><!-- .clear -->
         </div><!-- #content_grid -->
 </div><!-- #content -->

 <?php get_footer(); ?>
 }}}
 This looks for loop-service_archive.php which is as follow:
 {{{
 <?php
         global $wp_query;


         if ( get_query_var('paged') ) {
                 $paged = get_query_var('paged');
         } else if ( get_query_var('page') ) {
                 $paged = get_query_var('page');
         } else {
                 $paged = 1;
         }

         $args = array( 'post_type' => 'eb_service', 'posts_per_page' => 3,
 'orderby' => 'meta_value_num', 'meta_key' => 'order', 'order' => 'ASC',
 'paged' => $paged);

         $wp_query = new WP_Query( $args );


 ?>

 <?php if($wp_query->have_posts()): ?>
         <?php while( $wp_query->have_posts() ): $wp_query->the_post(); ?>
         <div id="service-<?php the_id(); ?>" class="grid_3">
                 <?php the_post_thumbnail('img_150x150'); ?>
                 <div class="service_title"><a href="<?php the_permalink();
 ?>"><?php the_title(); ?></a></div><!-- .service_title -->
         </div><!-- #service-<?php the_id(); ?> -->
         <?php endwhile; ?>

         <div class="clear"></div>
         <?php if($wp_query->max_num_pages > 1): ?>
         <div id="nav_below">
                 <?php posts_nav_link(); ?>
         </div>
         <?php endif; ?>

 <?php endif; ?>
 }}}

 Now my problem in detail. Everything works fine when I visit the url
 http://127.0.0.1/wordpress/service where wordpress is my install directory
 and 'service' is the slug for my custom post type 'eb_service'. As per my
 arguments specified in $arg (loop-service_archive.php), 3 posts from this
 post type are displayed along with the next page link. However, when I
 click on the next page link, i get a 404. When I print_r($wp_query) in my
 404.php i get the following information:
 ----
 WP_Query Object ( [query_vars] => Array ( [paged] => 2 [post_type] =>
 eb_service [error] => [m] => 0 [p] => 0 [post_parent] => [subpost] =>
 [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] =>
 [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0
 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] =>
 [tag_id] => [author_name] => [feed] => [tb] => [posts_per_page] => 10
 [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] =>
 [sentence] => [fields] => [category__in] => Array ( ) [category__not_in]
 => Array ( ) [category__and] => Array ( ) [post__in] => Array ( )
 [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array
 ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] =>
 Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results]
 => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1
 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC
 ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( ) [relation]
 => AND ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( )
 [relation] => ) [post_count] => 0 [current_post] => -1 [in_the_loop] =>
 [comment_count] => 0 [current_comment] => -1 [found_posts] => 7
 [max_num_pages] => 1 [max_num_comment_pages] => 0 [is_single] =>
 [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] =>
 [is_month] => [is_day] => [is_time] => [is_author] => [is_category] =>
 [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] =>
 [is_trackback] => [is_home] => [is_404] => 1 [is_comments_popup] =>
 [is_paged] => [is_admin] => [is_attachment] => [is_singular] =>
 [is_robots] => [is_posts_page] => [is_post_type_archive] =>
 [query_vars_hash] => ba5989721f3f39b579a3afc5262e51af [query_vars_changed]
 => [thumbnails_cached] => [query] => Array ( [paged] => 2 [post_type] =>
 eb_service ) [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM
 wp_posts WHERE 1=1 AND wp_posts.post_type = 'eb_service' AND
 (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
 ORDER BY wp_posts.post_date DESC LIMIT 10, 10 [posts] => Array ( )
 [queried_object] => stdClass Object ( [labels] => stdClass Object ( [name]
 => Services [singular_name] => Service [add_new] => Add New [add_new_item]
 => Add New Service [edit_item] => Edit Service [new_item] => New Service
 [view_item] => View Service [search_items] => Search Service [not_found]
 => No services found [not_found_in_trash] => No services found in Trash
 [parent_item_colon] => [all_items] => All Services [menu_name] => Services
 [name_admin_bar] => Service ) [description] => This is the post type to
 display services. [publicly_queryable] => 1 [exclude_from_search] =>
 [capability_type] => post [map_meta_cap] => 1 [_builtin] => [_edit_link]
 => post.php?post=%d [hierarchical] => 1 [public] => 1 [rewrite] => Array (
 [slug] => service [with_front] => [pages] => 1 [feeds] => 1 )
 [has_archive] => service [query_var] => eb_service [register_meta_box_cb]
 => [taxonomies] => Array ( ) [show_ui] => 1 [menu_position] => 6
 [menu_icon] => [permalink_epmask] => 1 [can_export] => 1
 [show_in_nav_menus] => 1 [show_in_menu] => 1 [show_in_admin_bar] => 1
 [name] => eb_service [cap] => stdClass Object ( [edit_post] => edit_post
 [read_post] => read_post [delete_post] => delete_post [edit_posts] =>
 edit_posts [edit_others_posts] => edit_others_posts [publish_posts] =>
 publish_posts [read_private_posts] => read_private_posts [read] => read
 [delete_posts] => delete_posts [delete_private_posts] =>
 delete_private_posts [delete_published_posts] => delete_published_posts
 [delete_others_posts] => delete_others_posts [edit_private_posts] =>
 edit_private_posts [edit_published_posts] => edit_published_posts )
 [label] => Services ) [queried_object_id] => 0 )
 ----

 Notice that it shows 'posts_per_page = 10' and 'paged = 2'. Now I get a
 404 because there are only 7 posts in my database tagged with post_type =
 eb_service. Thus retrieving 10 posts on page 2 for this post_type returns
 a 404 as there is nothing found in the query. However, I have explicitly
 mentioned posts_per_page = 3 in $arg while instantiating WP_Query class.
 Also, even if I change the post_type from eb_service to anything xyz in
 $arg while instantiating WP_Query class in my loop_service_archive.php the
 $wp_query dump from 404 error screen still shows post_type = eb_service.
 Thus my conclusion, that the arguments that I specify while instantiating
 WP_Query class in the archive page for a custom post are being ignored if
 the navigated page is 2 or more for that matter. Everything remains okay
 if the navigated page is 1.

 I hope this clarifies my problem. I have spent 4 days trying to debug the
 WP core and my code as well. Dont know where the problem lies :)

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/18562#comment:3>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list