Reputation: 13
I have a variable in WordPress (using Visual Composer plugin template)
$posts_query = $settings['posts_query']
and when i print_r
print_r($posts_query);
results on this:
size:4|order_by:date|post_type:donation_slider|post_status:publish
How can i get the field post_type?
I tried with $posts_query['post_type']
but it just shows letter "s" or $posts_query->post_type;
it shows blank!
Any thoughts?
Upvotes: 1
Views: 582
Reputation: 81
An alternative solution:
It appears that $posts_query
is a string so accessing it like an array won't pull out the 'post_type'
.
You could try using this function (Source: http://php.net/manual/en/function.explode.php), this makes a new array out of $posts_query
with |
being the separator between data fields and :
being the separator between key and value. Then you can get post_type
like $parsed_posts_query['post_type']
as shown.
function explode_with_keys($delim1,$delim2,$inputstring)
{
$firstarr = explode($delim1,$inputstring);
$finalarr = array();
foreach($firstarr as $set)
{
$setarr = explode($delim2,$set);
$finalarr[$setarr[0]] = $setarr[1];
}
return $finalarr;
}
$parsed_posts_query = explode_with_keys("|",":",$posts_query);
print_r($parsed_posts_query['post_type']);
Upvotes: 0
Reputation: 13
I found this solution:
$post_type_query = explode("|", $posts_query);
$post_type = str_replace("post_type:","",$post_type_query[2]);
and it print the value of post_type.
Thank you so much for your help guys, i appreciate that!
Upvotes: 0
Reputation: 26160
This is an atypical situation, and an atypical way to store information. It looks like someone decided to invent their own serialization storage (storing an array in a string).
To access the information you want, you'll need to manipulate the string into the various array components, using explode.
Here's some code to get you started:
// $posts_query is equal to "size:4|order_by:date|post_type:donation_slider|post_status:publish"
$parts = explode('|', $posts_query);
// now parts is an array: ['size:4', 'order_by:date', 'post_type:donation_slider', 'post_status:publish'
$array = [];
// loop over the exploded parts
foreach( $parts AS $part ) {
// split the sub-parts apart
$split = explode(':', $part);
// glue it into a proper associative array
$array[$split[0]] = $split[1];
}
Now you have an array that should look like this:
array(
'size' => 4,
'order_by' => 'date',
'post_type' => 'donation_slider',
'post_status' => 'publish'
)
Which you can access each separate part like so:
$post_type = $array[ 'post_type']; // returns "donation_slider"
Upvotes: 1
Reputation: 2328
Looks like an imploded string. The elements are separated by |
, and keys and values are separated by :
. You can use explode to create the array you want:
$array = [];
foreach( explode($posts_query, '|') as $keyVal) {
$tmp = explode($keyVal, ':');
$array[$tmp[0]] = $tmp[1];
}
Then you can use array like you did in your question.
Upvotes: 0
Reputation: 20737
I do not know WordPress very well but the native PHP way to do this is like this:
$posts_query = 'size:4|order_by:date|post_type:donation_slider|post_status:publish';
foreach( explode( '|', $posts_query ) as $pairs )
{
if( explode( ':', $pairs )[0] === 'post_type' )
{
echo explode( ':', $pairs )[1];
break;
}
}
Upvotes: 0