I’m still quite new to PHP & MySql and I’m not sure whether this would be acheived as SQL or PHP.

I have the following query:

SELECT wp_posts.ID,
    wp_postmeta.meta_value
FROM wp_posts 
    INNER JOIN wp_postmeta
       ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_type = 'cooney_properties'
AND wp_posts.post_status = 'publish'
AND (wp_postmeta.meta_key = 'longitude' OR wp_postmeta.meta_key = 'latitude')

This is from my WordPress database and as you can see the longitude and latitude values are under the same column “meta_key”.

The array from the above query gives me similar results to the below (not exact just rough example):

Array
    (
     [0] => stdClass Object
    (
        [ID] => 25016273
        [meta_value] => 51.01454
    )
     [1] => stdClass Object
    (
        [ID] => 24617570
        [meta_value] => 51.01447
    )
     [2] => stdClass Object
    (
        [ID] => 24780750
        [meta_value] => 51.01535
    )
     ------------------- (later on items with same ID) -------------
     [141] => stdClass Object
    (
        [ID] => 24617570
        [meta_value] => -3.107139
    )
     [142] => stdClass Object
    (
        [ID] => 24780750
        [meta_value] => -3.096807
    )
     [143] => stdClass Object
    (
        [ID] => 24764956
        [meta_value] => -3.104187
    )
    )

Being from the same column of the database I don’t know if its possible to fix in my query or whether its a PHP fix, I need either the items with same ID to be in the same array and possibly the renaming of the keys, if possible like below:

 Array
    (
     [0] => stdClass Object
    (
        [ID] => 25016273
        [longitude] => 51.01454
        [latitude] => -3.107139
    )
     [1] => stdClass Object
    (
        [ID] => 24617570
        [longitude] => 51.01447
        [latitude] => -3.096807
    )
     [2] => stdClass Object
    (
        [ID] => 24780750
        [longitude] => 51.01535
        [latitude] => -3.104187
    )
   )

Read more here: MySQL query and array output from same database column


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , ,

Wordpress related questions and answers: