Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#18562 closed defect (bug) (invalid)

WP_Query class ignores all arguments in custom post type

Reported by: arsalkeen's profile arsalkeen Owned by:
Milestone: Priority: normal
Severity: critical Version: 3.2.1
Component: General Keywords: close
Focuses: Cc:

Description

WP_Query is ignoring all the arguments that I send while instantiating it in a custom post type archive page. It works fine when pagination link is 1, but it completely ignores all the arguments when pagination link is /page/2

However, when I use the same code as a template, it shows everything correctly. It only ignores the arguments on the custom post archive page.

Change History (9)

#1 @dd32
13 years ago

Can you please post some minimal code which can be used to demonstrate the problem you're having?

#2 @dd32
13 years ago

  • Keywords reporter-feedback added

#3 follow-up: @arsalkeen
13 years ago

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] => [categoryin] => Array ( ) [categorynot_in] => Array ( ) [categoryand] => Array ( ) [postin] => Array ( ) [postnot_in] => Array ( ) [tagin] => Array ( ) [tagnot_in] => Array ( ) [tagand] => Array ( ) [tag_slugin] => Array ( ) [tag_slugand] => 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 :)

#4 @DrewAPicture
13 years ago

  • Cc xoodrew@… added

I'm not sure if this is fully related to your problem, but I had an issue kind of similar to what you're describing last week. The way I went about fixing the pagination problem on my CPT archive was to set 'rewrite' => true and leave 'has_archive' => 'slug' alone. Then WordPress rendered my CPT archive file of archive-post_type.php as /slug/ and /slug/page/2 etc. just fine. I found that using a rewrite array in register_post_type was causing the problem with pagination.

Version 0, edited 13 years ago by DrewAPicture (next)

#5 in reply to: ↑ 3 @SergeyBiryukov
13 years ago

Replying to arsalkeen:

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.

That looks like a duplicate of #16168.

#6 @scribu
13 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Yep, same problem as #16168, with same solution: http://core.trac.wordpress.org/ticket/16168#comment:5

#7 @scribu
13 years ago

  • Keywords reporter-feedback removed

#8 @arsalkeen
13 years ago

hmmm... I guess the filter approach should work. I myself was trying this as it made sense the default query was building up before my custom one and hence a 404, however I was unable to find the proper filter/action hook for this. And one more thing, using this approach, I will have to place my add_filter() call in the functions.php right...??? I am not sure that I am comfortable with that.

Anyways... will try this in the morning... its 1:25 AM here and I am dizzy... its eid holidays here :) I hope it does work :)

Thanks in advance everyone...

#9 @arsalkeen
13 years ago

  • Keywords close added

Ok great... its working... and in my functions.php I use a conditional statement to apply this filter if the $requestpost_type? = 'eb_service' and is_archive().

This should avoid any performance issues I guess. Need to test this though :)

Note: See TracTickets for help on using tickets.