• Resolved sintax63

    (@sintax63)


    I am trying to pass a variable into a query_posts( ) but for some reason it doesn’t seem to be taking effect. I can echo the variable all over the place, and use it in places other than the query_posts( ), but not inside.

    To be as brief as possible, I have a “Featured Article” on my main page, which is the newest post to my blog. I am trying to grab both the Category ID and Category Name from this and pass it down the page a bit to an “Recently In category_name” type of deal.

    Here is the code from my single Featured Article area:

    <?php query_posts('showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <?php $category = get_the_category();
    $featured_NAME = $category[0]->cat_name;
     $featured_ID   = $category[0]->cat_ID;
    ?>

    Now down in the “Recently In category_name” I am using the following:

    <?php query_posts('showposts=3&cat=$featured_ID'); ?>
    <h3>Recently In <?php echo $featured_NAME; ?></h3>
    
    <?php while (have_posts()) : the_post(); ?>

    The $featured_NAME inside my <h3> tags works great, but the query_posts( ) is completely ignoring my $featured_ID variable. It is just reading as “cat=” and showing the last 3 entries in all categories.

    I’ve read through the docs and tried numerous things, but just can’t get it to work.

    Any suggestions?

Viewing 5 replies - 1 through 5 (of 5 total)
  • <?php query_posts(‘showposts=3&cat=’.$featured_ID); ?>

    basically you want to keep the variables out of the single quotes, because anything inside those quotes is treated as literal text and not processed further.

    Thread Starter sintax63

    (@sintax63)

    Works great, Ivovic –
    Thanks so much for the help!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    That will work. Alternatives that will also do the trick:

    Double quoting strings makes it process variables inside the string:
    <?php query_posts("showposts=3&cat=$featured_ID"); ?>

    WordPress also supports explicit arrays instead of query strings:

    <?php query_posts(array(
    'showposts'=> 3,
    'cat'=> $featured_ID,
    )); ?>

    I prefer to use arrays for everything now because it’s more explicit as to what is being set to what, and it’s faster as there’s less string processing going on.

    Faster in whatway Otto? do you mean the regular query function is not straight enough?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Faster because all the functions in WordPress that take query string style parameters use arrays internally anyway.

    See, if you call query_posts(‘foo=bar’), then it eventually calls a function called parse_str to turn ‘foo=bar’ into array(‘foo’=>’bar’).

    But if you use an array in the first place, the call to parse_str never happens. Obviously, this is faster.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Passing Variable To query_posts’ is closed to new replies.