WordPress.org

Ready to get started?Download WordPress

Forums

Using a variable inside WP_Query (5 posts)

  1. thejester12
    Member
    Posted 5 years ago #

    Howdy, wondering how I can use a php variable that I have predefined in the middle of a wp_query statement? Basically I'm creating a custom "related posts" kind of thing that finds posts with shared tags. (Only one specific tag is stored in the variable.) Here's the code I'm working with below:

    <?php $posts_query2 = new WP_Query('tag=news+$myvariable&paged=$paged&showposts=4'); ?>

    The statement works fine when I take away the variable and test it by putting in the exact statement that I want to be there. I also know the variable is working fine by doing an echo statement in the post.

    I'm using a php variable elsewhere to pull up related links, and it works fine in the wp_list_bookmarks statement, but for some reason its not working in wp_query, any ideas why?

  2. thejester12
    Member
    Posted 5 years ago #

    Is this possible? Seems like it should be simple?

  3. thejester12
    Member
    Posted 5 years ago #

    Does it have something to do with global variables and whatnot?

  4. renato_s
    Member
    Posted 5 years ago #

    Try to use double quotes instead of single ones to wrap the query parameters.

  5. thejester12
    Member
    Posted 5 years ago #

    Wow, so simple... That's why as a designer I get frustrated with PHP/Programming stuff sometimes... Syntax gets me every time!

    Thanks so much! It worked!

Topic Closed

This topic has been closed to new replies.

About this Topic