Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

  • Making the var into a CONSTANT, just like posts_per_page (which doesn't cause a problem).
  • Concating quote around the $category_id number - The result is the same as no quotes at all. It doesn't work.
  • 'cat' => array($category_id) - I pulled an error - It didn't like
    that it was an array.

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?

Help. :)

share|improve this question

4 Answers 4

Where/how are you defining $category_id?

Reference the Codex entry for WP_Query() category parameters. WP_Query() expects category IDs to be passed as integers, not as strings:

  • If $category_id is an integer, pass it to 'cat'.
  • If $category_id is an array (of category IDs, again as integers), pass it to 'category__in'
share|improve this answer
    
Thanks @Chip. This line, right above the query: $category_id = get_cat_ID(single_cat_title('', false)); If I echo it to screen, it's there and it looks kosher to me. In fact, the query_vars DOES list 'cat'=> the problem is, the per_page and the post_types go wonky. I'm not pulling an undeclared var or type being wrong. As far as I can tell that's not the issue. Any other suggestions? Thx again –  Chief Alchemist Jan 30 '13 at 15:34
    
And what does that line return? Try a var_dump( $category_id ). –  Chip Bennett Jan 30 '13 at 15:37
    
var_dump( $category_id ) returns: string(1) "7". OH? –  Chief Alchemist Jan 30 '13 at 16:01
    
I added / tested this: $category_id = (int)get_cat_ID(single_cat_title('', false)); var_dump( $category_id ); and WP_Query ignores my args and defaults to something else. Good try but best I can tell the type (string or int) is not the issue. And if I wrap the int $var in '"'.$var.'"' it does NOT work. BUT '"-'.$var.'"'; does. NOT = OK, else my args are dropped and it goes to some defaults (?) –  Chief Alchemist Jan 30 '13 at 16:07
    
So, just as a longshot, try type-casting $category_id as an integer? –  Chip Bennett Jan 30 '13 at 16:25

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 $category_id :

global $post;
$category_id = get_the_category($post->ID)[0]->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.

share|improve this answer
    
Thx @Simon. Allow me to clarify a bit. The 'cat' => $var does work (kinda). I shows up in the query_var. However, when using a $var to do that assignment, other things go wonky (e.g. posts_per_page, post_type, etc.). I'm not getting an error. What's happening is the arg array I us in WP_Query() doesn't match what I see in the print_r() *when I try to assign 'cat' => with a $var. Yeah, wrap your head around that :) Also, I'm trying to use the line of code you mention and I'm pulling: Parse error: syntax error, unexpected '[' . ?? –  Chief Alchemist Jan 30 '13 at 15:44
    
I add the global $post, i guess this is what throws the parse error, sorry. I did not notice on my tests anything wrong except the empty $category_id. Just a note, I don't use print_r which is difficult to read, I use the console plugin which allow you to see variables in the firebug console. It's very handy and makes reading much easier –  Simon Jan 30 '13 at 16:06
    
Well...I had added the global $post; myself the first time and still pulled the error. I guess that's why I was asking. Strange, right? –  Chief Alchemist Jan 30 '13 at 20:23
    
Ok, this should work only on a single page, not category page, sorry I missed that ! It's interesting, but there is probably an explaination to your problem. –  Simon Jan 30 '13 at 20:59
    
I checked your original code in a category.php page and it's working properly. No mess in post_type or post_per_page. –  Simon Jan 30 '13 at 21:11

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);
share|improve this answer
    
Hi @Wyck - Yes, that is what I'm tending to believe. That is, there's something somewhere else. I presume that because the default values (i.e. what my arg array gets ditched for some other set of query_arr values) have to come from somewhere, eh? But that's what I'm jamming the we_reset on top. Push some to shove that should leave me with a clean slate, yes? Even so, why would setting 'cat'=> (and far as I can tell, only that parm) with a $var be an issue. That level of weirdness feels like a big to me. You? All that said...any thoughts on a solution? Or how to find the theme's bug / hole? –  Chief Alchemist Jan 30 '13 at 20:27
    
Check that template page for other loops or it is including other parts of templates (include or get_template_part), disable them and see if it works, remove widgets from loading, remove plugins, check if anything is hijacking the query via pre_get_posts, check if anything else is using your $my_query (change the name). –  Wyck Jan 30 '13 at 22:14

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.

regards Andy

share|improve this answer
    
Thx @Andy Killen. Sad to say it did not help. At this point it much be something somewhere else in the theme. That said, I would expect the wp_reset_query() to give me a clean slate, yes? As it is, 'cat' being assigned with a $val causes WP to seek some other set of args. Those DO look like a set the site is using elsewhere. What I don't understand is why. Why is not using the args I'm asking it to use? And better still, since I'm not pulling any real errors, how should I do abuot trying to pin down this "leak"? Any suggestions would be greatly appreciated. Thank you! –  Chief Alchemist Jan 30 '13 at 20:42
    
I agree with Wyck (currently below), this is most likely an issue with another plugin or your theme. I would try disabling all plugins and trying again, and then if that does not do the job, try putting you categories.php into the twentyten theme or similar and trying there. The wp_reset_query() is not so vital in this situation. You could look at this as being a nested loop inside the standard loop if you really wanted, as you are using your own var to store the loop it won't matter so much. –  Andy Killen Jan 30 '13 at 20:48
    
What if I just did a proper SQL query? That's not going to add any extra overhead, etc., correct? It just seems to me that by the time I track down the source of the "leak" - and I might never be able to do that since the use of a $var triggering a "bug" is just plain odd to begin with, eh - I could just tap out a SQL statement and not have to worry about WP_Query() and its issues. –  Chief Alchemist Jan 30 '13 at 21:37
    
Actually, I think my Plan A for now is going to be a string with all the cats not'ed. And then I'll remove the current cat. Opps, I think I already mentioned this as a solutions. Crazy but I'll work. And if I find the hole someday, I can just remove this "hack". –  Chief Alchemist Jan 30 '13 at 21:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.