For the sake of this discussion, here’s a version of my query within category.php:
wp_reset_query(); $category_id = get_cat_ID(single_cat_title('', false)); $my_query = new WP_Query(array( 'posts_per_page' => SOME_DEFINED_VALUE, 'cat' => $category_id, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), 'post_type' => array('post','post_custom_1','post_custom_2','post_custom_3') )); // print_r($my_query);
In short, it doesn’t work. Here’s what I’ve noticed.
When I do the print_r($my_query); I can see the query_vars in the first line. They do not match my array args. For example, the posts_per_page defaults to some other value (not the CONSTANT), and the post_type list no longer includes post (which is should / must).
If I remove the line with ‘cat’=> or hardcode the value (‘cat’=> 3) then every thing works as expected. The remaining query_vars show up in the print_r. Life is good 🙂
I have tried the following but found no success:
Anyone have any suggestions?
Now here’s what did “work” (read: I could use my $category_id and doing so didn’t muck up the rest of the args in the WP_Query() array list).
'cat' => '"-'$category_id'"',
That is, I could not that cat ID. Crazy, right?
Unless I can resolve it properly, I’m going to have a comma separated sting of all my cat not’ed, parse out the current cat and then use that string to do the query. In other words if my cats were A, B and C, instead of doing a query for A (as one would expect to do)
'cat' => A
I’m going to query for not B, not C.
'cat' => -B,-C
Hopefully there’s a less hack-y solution. I’ve been on this too many hours now and I’m desperate enough to use this hack and be done with it. That said, this sure feel like a bug in the core to me. Yes, I’m using 3.5.1 (or are we up to .2 or .3 now?). My point is, I’m on the latest version (and this did the same in 3.2.x).
btw, I’ve seen this issue posted elsewhere (e.g. WP forums). The one solution suggested the args array not be an array but a string. Even if that’s possible with WP_Query, how would I do the list / array for the post_types as a string? Finally, if I’m doing something wrong then the Codex help page needs an update, eh?
Where/how are you defining
Reference the Codex entry for
WP_Query() category parameters.
WP_Query() expects category IDs to be passed as integers, not as strings:
$category_idis an integer, pass it to
$category_idis an array (of category IDs, again as integers), pass it to
I guess it has to do with the way you get the category. I did set up the function on a blog i’m working on ;
$category_id is always 0.
So to check I use this line for
global $post; $category_id = get_the_category($post->ID)->term_id;
Just a quick way to get the first category assigned to a post.
After that, the query was ok and the
catparameter on the query gets populated.
I tested your function and it works fine, so you probably have an error with some other code in your theme or a plugin conflict. Or maybe you loop is not correct.
For reference I tested the following code on
category.php in twentyeleven, the output was correct, without errors.
wp_reset_query(); define("SOME_DEFINED_VALUE", 5); $category_id = get_cat_ID(single_cat_title('', false)); $my_query = new WP_Query(array( 'posts_per_page' => SOME_DEFINED_VALUE, 'cat' => $category_id, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), 'post_type' => array('post','post_custom_1','post_custom_2','post_custom_3') )); var_dump($my_query);
When I spilt up your thing into manageable chunks to analyze them I found that the problem was most likely with the “paged” line in the array.
wp_reset_query(); $category_id = get_cat_ID(single_cat_title('', false)); $paged = ( get_query_var('paged') )? get_query_var('paged') : 1 ; $query = array( 'posts_per_page' => 5, 'cat' => $category_id, 'paged' => $paged , 'post_type' => array('post','custom_1','custom_2','custom_3') ); $loop = new WP_Query( $query); print_r($loop);
As you can see I added an extra bracket around the first
get_query_var so that it became a proper ternary operator. I tried this on my test rig and it worked just fine.