I have some common meta keys on every post.

Ex: hotel_person, hotel_number All the posts have these two meta keys.

I want to show these meta key values and the post titles. All these values and post title should be able to shown in admin panel. (All the posts are hotels. no need to consider the category)

Eg: If I have two hotels in my site, i want show like below in my admin panel.

Hotel 1


Hotel 2

I tried something but it's not working.

[APPENDED - What I've tried] first I added an admin menu.

add_action( 'admin_menu', 'hotel_contact' );
function hotel_contact(){
    add_menu_page( 'Hotel Contact', 'Hotel Contact', 'manage_options', 'hotel_contacts', 'hotel_contact_function', plugins_url( 'myplugin/images/icon.png' ), 7 ); 

Then I created the function to get the hotel contact number

function hotel_contact_function() {         
    echo get_post_meta( 3, 'hotel_contact_number' , true ); 

It gives me 3rd hotels(post ID) contact number when I visit to the admin menu Hotel Contact Then I've tried to get all post meta data for the meta key hotel_contact_number. So I've changed the function hotel_contact_function to:

function hotel_contact_function() {         
    $contacts = get_post_meta( get_the_ID(), 'hotel_contact_number' , true ); 
        foreach ($contacts as $contact) {
            echo $contact.'<br/>';

But it gives a warning when I visit to the admin menu Hotel contact : Warning: Invalid argument supplied for foreach() in /home/cyberact/public_html/mysite.com/wp-content/themes/u-design-child/functions.php on line 601

  • What did you try so far? We can work through it. – Andrew Bartel Nov 7 '14 at 19:35
  • @AndrewBartel Please have a look at my updated answer. – Riffaz Starr Nov 8 '14 at 3:52
  • If you want to do foreach, your $contacts should return an array. Now it is returning a string. Change your 3rd argument to false. You should do print_r( $contact ) instead of echo as well to show available values. – SLH Nov 8 '14 at 9:35
  • @SLH thank you for that. I changed the code to : function hotel_contact_function() { //echo get_post_meta( 213, 'hotel_contact_number' , true ); $contacts = get_post_meta( get_the_ID(), 'hotel_contact_number' , false ); foreach ($contacts as $contact) { print_r ($contact).'<br/>'; } } but still the same warning message I am getting. – Riffaz Starr Nov 9 '14 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.