0

I am trying to print this word press query, but the variable doesn't work in that array (these numbers are just example). It will just take first number 53 and not the other numbers.

$catfeaturenumbers = '53,57,4294,71';
$categoryfeatured = array($catfeaturenumbers);
$featured = array(
'post__in' => $categoryfeatured
);
1
  • That does not create the array you think it does. Do print_r($categoryfeatured); to see the array structure and after that you probably want to look at explode(). Commented Oct 17, 2016 at 18:16

1 Answer 1

1

I think you want something like below:-

<?php

$catfeaturenumbers = '53,57,4294,71';

// create an array through explode

$categoryfeatured = explode(',',$catfeaturenumbers);

echo "<pre/>";print_r($categoryfeatured); // print array to check
$featured = array(
 'post__in' => $categoryfeatured
);

Note:- if you want to use your original string into an IN QUERY(example SELECT * FROM <table> WHERE id IN (your original string data)) then do like below:-

$categoryfeatured = "'".implode("','",explode(',',$catfeaturenumbers))."'";

echo  $categoryfeatured;

Both code Output link:-https://eval.in/662140

Sign up to request clarification or add additional context in comments.

2 Comments

It worked ! thank you very much, even if I don't understand logic behind it.
It's simple, you need to pass an array to your query, you have string . so you have to convert it to array first.So explode() did that work.

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.