WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I try to display posts from specific category only in a author page, using get_option function like in the code below but it does not work:

<?php query_posts('cat='.get_option('admin_cat')); ?>

However, if using the id number, works perfect.

<?php query_posts( 'cat=6' ); ?> 

How can I use get_option function instead of ID number?

Later edit:

I apologize, it was my mistake that I wrote in admin option the category name instead of its id. However a code would be helpful to read both variants, like:

if admin option the category is an id
display its id
if in admin option category is a name
display its name


share|improve this question
in any case you shouldn't use query_posts. try doing a var_dump of your get_option call – paul Jun 9 '13 at 12:56
up vote 1 down vote accepted

You can't use the category name to search for categories. You can use the slug. Be aware of the difference. A slug is normalized to lower case with spaces replaced by dashes. The slug for "Foo A" would be "foo-a". Using "Foo A" will not work.

$admin_cat = get_option('admin_cat');

if (ctype_digit("$admin_cat")) { // this is an ID
  $qry = array('cat' => (int)$admin_cat);
} else {
  $qry = array('category_name' => $admin_cat);
$my_query = new WP_Query($qry);

As stated in a comment to your question, do not use query_posts.

It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

http://codex.wordpress.org/Function_Reference/query_posts (emphasis mine)

share|improve this answer
Very elaborate and very useful. Thanks a lot! – Knott Jun 9 '13 at 22:27

Your Answer


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.