0

I have:

  • Wordpress custom post types;
  • Two hotels registered in the custom Wordpress admin area;
  • A variable named $hotels which get the field where the hotels registered are listed;

For the $hotels, I'm using the get_field property that is works like an array. Inside this array, I have to get the name of the post (which is the name of the hotel). I'd like to search into the array the name of the hotel and print it in the page.

If I code:

<?php
  $hotels = get_field('produtos_hotel');
  print_r($hotels);
  $key = array_search('Ocean Maya Royale', $hotels);
  echo $key; // should display 'Ocean Maya Royale';
?>

It will output this mess:

Array (
    [0] => WP_Post Object (
        [ID] => 1113
        [post_author] => 1
        [post_date] => 2014-06-11 16:18:59
        [post_date_gmt] => 2014-06-11 19:18:59
        [post_content] => 
        [post_title] => Ocean Maya Royale
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] => 
        [post_name] => ocean-maya-royale
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2014-06-18 14:41:25
        [post_modified_gmt] => 2014-06-18 17:41:25
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => http://localhost/mydocs/advtour/newsite/wordpress/?post_type=add_content&p=1113
        [menu_order] => 0
        [post_type] => add_content
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

    [1] => WP_Post Object (
        [ID] => 1302
        [post_author] => 1
        [post_date] => 2014-06-12 01:19:36
        [post_date_gmt] => 2014-06-12 04:19:36
        [post_content] => 
        [post_title] => Flamingo Cancun Resort
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] => 
        [post_name] => flamingo-cancun-resort
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2014-06-18 14:40:19
        [post_modified_gmt] => 2014-06-18 17:40:19
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => http://localhost/mydocs/advtour/newsite/wordpress/?post_type=add_content&p=1302
        [menu_order] => 0
        [post_type] => add_content
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )
)

Can you guys see the [post_title] element? So, I have to take the Ocean Maya Royale or the Flamingo Cancun Resort, but the array_search can not find it!

Thank you!

2
  • Should be $hotels['post_name'] Commented Jun 20, 2014 at 14:02
  • Err, $hotels[$id]['post_name'] Commented Jun 20, 2014 at 14:09

3 Answers 3

1
$srch = 'Ocean Maya Royale';
foreach ($hotels as $key => $val) {
    $key1 = array_search($srch, $hotels[$key]);
    echo $key . ' ' . $key1 . chr(10) . '<br />';
}

It is a second level array and hence needs to be iterated thru. If the search string is to be found in the specific second subscript of 'post_title' it can be hardcoded and the first subscript alone can be returned like.

$srch = 'Ocean Maya Royale';
foreach ($hotels as $key => $val) {
    if ($srch == $hotels[$key]['post_title'])
        echo $key . chr(10) . '<br />';
}
Sign up to request clarification or add additional context in comments.

Comments

0

array_search gives you the key for the array, not the variable. So to get the title use this variable:

$theTitle = $hotels[$key]->post_title;
echo $theTitle;

So array_search, when looking for that title, will return the key of 0, because that's which element has that title within it.

//EDIT//

Because you have an array of objects, you need to change your array_search to account for the objects:

array_search(array('post_title' => 'Ocean Maya Royale')

5 Comments

Yeah, that works fine! It got the objects inside the array! Now I'm able to customize it in my away! Thank buddy! =D
What inspired you to changing from wrong answer $theTitle = $hotels[$key]['post_title']; to correct?
Reading the question a second time. Didn't account for the object in the array. You make it sound like you'd prefer the wrong answer :)
But when I used echo $theTitle; it was able to show the hotel name! So, what is wrong with this?
@CaioFerrari Nothing wrong, just that the accepted answer changed completely from wrong to correct, as soon I posted my $hotels[1]->post_title; now self-deleted answer.
0

The basic form of filtering using array_reduce:

array_reduce( $hotels, function($result, $item) {
    if ($item->post_title == 'Ocean Maya Royale') {
        $result[] = $item;
    }
}, array());

You can also make it into a function for reusability:

function searchPostTitle($posts, $search_title)
{
    array_reduce( $posts, function($result, $item) use ($search_title) {
        if ($item->post_title == $search_title) {
            $result[] = $item;
        }
    }, array());
}

Comments

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.