0

Slight problem, I have the following code:

$purchaseprodid = $_GET['ids'];
$args = array(
                  'post_type' => 'prizes',
                  'post__in' => array($purchaseprodid)
                  );
                query_posts($args);

                while (have_posts()) : the_post(); 

I am getting comma seperated numbers for id's in a url, and they dont seem to be querying the posts. If I manually type the id's in they work no problem, but just not when using a variable $purchaseprodid.

Any ideas?

Thanks!

2 Answers 2

2

You need to explode the string you obtain from $_GET['ids'] into an array, at the moment you a parsing a to string post__in rather than an array of IDs.

Try

$purchaseprodid = isset($_GET['ids']) ? explode(',',$_GET['ids']) : array();

However, you can sometimes run into difficulties using $_GET with WordPress, it's better to use the API provided and register your variable, see this question.

0
1

You can instead use the standard WordPress function wp_parse_id_list like this:

$purchaseprodid = isset( $_GET['ids'] ) ? wp_parse_id_list( $_GET['ids'] ) : array();

that checks that the input data is not already an array, clears duplicates and converts the type of each array element to integer.

More info at:

https://developer.wordpress.org/reference/functions/wp_parse_id_list/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.