Skip to main content
added 324 characters in body
Source Link

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. :)

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).

Help. :)

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. :)

Source Link

WP_Query and using a variable for 'cat'=> in the args array = WP Bug?

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).

Help. :)