I don't $_GET this, please help!
April 28, 2009 1:22 PM   Subscribe

WordPressFilter: Trying to display posts based on "GET" value in URL. I'm so close but just ran into a brick wall. Help?

Let me start off by saying I know very little about WordPress and even less about PHP. I'm trying to integrate WordPress into an already existing site, so I'm avoiding the use of templates, in favor of "the Loop."

I've utilized the "query_posts" command from within the loop to consistently retrieve posts based on id number. Here's an example of what my code looked like (sans carrots):

?php
// retrieve one post with an ID number 3
query_posts('p=3');
?

Now, I'm trying to incorporate a way to pull that ID number from the URL. So if someone were to visit http://site.com?postnum=10, they would be able to see post #10. I'm no PHP expert but here's what I came up with (sans carrots):

?php
// retrieve one post with an ID number from URL
$postnum=$_GET["postnum"];
query_posts('p=$postnum');
?

This didn't work and hopefully you know why. When I use this code, it basically displays all of my posts instead of just the one specified in the URL.

If you've followed me thus far, I'd appreciate your suggestions. Be gentle!
posted by siclik to Computers & Internet (7 answers total)
 
Best answer: It's been a loooong time since I've used PHP but if memory serves, PHP only interpolates (changes $postnum into a variable) for double-quoted strings, not single-quoted strings. So try

query_posts("p=$postnum");

instead.
posted by coryinabox at 1:28 PM on April 28, 2009


coryinabox is absolutely right.

You could do this:

?php
// retrieve one post with an ID number from URL
$postnum=$_GET["postnum"];
query_posts('p=' . $postnum); //this is now correct
?
posted by bshort at 1:34 PM on April 28, 2009


Response by poster: You, sir, are my hero. Thank you.

bshort: that's a great option as well - thanks!
posted by siclik at 1:35 PM on April 28, 2009


coryinabox is correct. Here is the PHP manual on this topic.

Are you sure the value of the p parameter in _GET is accurate? If WordPress is set to to have search engine-friendly URLs, PHP probably won't ever get that in $_GET. Within WordPress there appears to be a function url_to_postid($) that does just what it says if permalinks are on.
posted by mkb at 1:42 PM on April 28, 2009


bshort's option is the best, using single quotes and concatenating your values makes sure that your variables with strings are very explicit. Also, it's slightly faster than using double quotes because PHP doesn't need to interpret the string, which is a minor benefit, but adds up if you are doing a lot of it. :)
posted by Allenthar at 3:35 PM on April 28, 2009


If you aren't doing anything with the $postnum variable a third simpler way would be
query_posts("p={$_GET['postnum']}");

The " 's as explained above tell PHP to parse the string, and the {}'s are needed around array variables (although I think with newer versions of PHP this is no longer necessary?).

Technically using single quotes and concatenation can offer marginal benefits (although this may be offset anyway by declaring a variable for no reason)? This is really just an optimization to increase readability.
posted by xorry at 6:47 PM on April 28, 2009


I recommend that you change this:

$postnum=$_GET["postnum"];

to this:

$postnum=intval($_GET["postnum"]);

Just to cut off a possible attack vector.
posted by qvtqht at 10:52 AM on May 1, 2009


« Older Where's my jetpack?!   |   Does a written agreement entitle me to a kill fee? Newer »
This thread is closed to new comments.