All Custom Field Values AND Page Titles
-
Hello. I am trying to accomplish the following. I have 40 to 50 pages (not posts) each has information about a store ie a phone number in custom fields. So Store1 (page title), Phone_Number (key), 888-8888 (value). I have found what I believe to be the correct way to display all values of Phone_Number below:
<?php $values = $wpdb->get_col("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'phone_number'" ); ?> <ul> <?php foreach($values as $phone_number) { echo ' <li>'.$phone_number.'</li> '; } ?> </ul>
The problem is that after I proudly echoed out this list, I realized that it doesn’t do much good without each Store Name with it. So my question is, if I wanted to have a page just to list the phone numbers of all the stores dynamically–using/modifying/changing the above code how would I get each individual Page Title to display alongside/with each value?
Would like it to simply look like:
Store1 888-8888
Store2 555-4444
Store3 444-6666
etcThank you very much in advance, DF
-
Here is some sample code that might help. You will need to add divs for proper styling.
<?php $meta_key = 'phone_number'; $args = array( 'posts_per_page' => -1, 'ignore_sticky_posts' => 1, 'meta_key' => $meta_key, 'orderby' => 'title', ); $my_posts = new WP_Query( $args); if ($my_posts->have_posts()) { ?> <ul> <?php while ($my_posts->have_posts()) { $my_posts->the_post(); $phone = get_post_meta($post->ID, $meta_key, true); ?> <li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a> <?php echo $phone; ?></li> <?php } ?> </ul> <?php } ?>
vtxyzzy,
First, thank you for taking the time to try and help me. I tried plugging in this code and it doesn’t seem to work. I am admittedly not a PHP coder, so I really don’t know where to begin to make it work. I hate to ask for favors as you already have gone out of your way, but in an effort to learn instead of simply asking for an answer, could you explain the above code and what it is trying to do? Thank you very much again, DF
The code I gave probably is a direct replacement for the code you showed. I can’t be sure because I need to know a few more lines (just 3 or 4) below what you showed.
I added comments to help explain the code”
<?php $meta_key = 'phone_number'; // Meta_key to select $args = array( 'posts_per_page' => -1, // Get all posts 'ignore_sticky_posts' => 1, // Don't put sticky posts at top 'meta_key' => $meta_key, // Only posts with this meta_key 'orderby' => 'title', // Show the posts alphabetical by title rather than date ); $my_posts = new WP_Query( $args); // Get the posts if ($my_posts->have_posts()) { // If any posts were found?> <ul> <?php while ($my_posts->have_posts()) { // Loop through the posts $my_posts->the_post(); // Set up the data for this post $phone = get_post_meta($post->ID, $meta_key, true); // Get the phone number?> <li><a href="<?php the_permalink(); // Link to the title ?>" rel="bookmark"><?php the_title(); ?></a> <?php echo $phone; ?></li> <?php } // End the Loop?> </ul> <?php } // End the if test?>
I wanted to update my findings for those who may follow, and also ask for additional help as I am getting very close to reaching my goal. Original question: many pages as a list on one page, each list item as a link and ALL Custom Field values. So I have a page titled Stores, need list of all stores; AlphaStore, BravoStore, CharlieStore etc. (with titles as links to their respective pages) and would like all custom fields displayed under each. I have solved 90% by using this:
<?php $mypages = get_pages( array( 'child_of' => 8, 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) );//This lists all pages that are a child of '8' which is the parent Stores. Sorted by post_title, alphabetically foreach( $mypages as $page ) {//This loops through the array values individually ?> <h4><a href="<?php echo get_page_link($page); ?>"><?php echo $page->post_title; ?></a></h4> //get_page_link($page) inserts correct link address to individual pages //$page->post_title lists page <div class="entry"><?php echo $content; ?></div>//$content is nothing yet, where I need help <?php } ?>
So I have my list of all pages that are a child of Stores, alphabetically, as links, and linked to the proper address. The only thing that I cannot figure out is how to get ALL Custom Field values to show up the following <div> as the $content variable. Here is the odd part, if I insert
$content = get_post_meta($page->ID, 'phone_number' , true);
just above the foreach loop, it will return all the matching phone numbers for each store. However, when I simply use get_post_meta() which according to the Codex is supposed to return all values, it simply reads ‘array’ in black text, not the values. I feel that I am VERY close, if somebody could help me out and bring this month long journey to an end it would be greatly appreciated.get_post_meta()
does return all values, in an array. You must loop through the array and display only the values you want to show.$meta_values = get_post_meta(); $keys_to_show = array('auteur', 'Favorite Fruits', 'competitor_1'); // array of keys to show foreach ($meta_values as $key=>$values) { if( in_array($key, $keys_to_show) ) { foreach ($values as $value) { echo "$value<br />"; } } }
vtxyzzy, thank you once again for your response. Here is what I’ve got:
<?php $mypages = get_pages( array( 'child_of' => 8, 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) ); foreach( $mypages as $page ) { $meta_values = get_post_meta($page->ID); $keys_to_show = array('phone_number', 'fax_number', 'regular_mail'); // array of keys to show foreach ($meta_values as $key=>$values) { if( in_array($key, $keys_to_show) ) { foreach ($values as $value) echo "$value<br />"; } } ?> <h4><a href="<?php echo get_page_link($page); ?>"><?php echo $page->post_title; ?></a></h4> <div class="entry"><?php echo $value; ?></div> <?php } ?>
But it is not working. It echoes the list perfectly above the link/title from the original echo statement and returns all three Custom Field values of phone_number, fax_number, and regular_address. However, I can’t seem to get it down where it needs to go! So close!! The bottom
echo $value
only returns the last value. I don’t get it but I almost certain it has something to do with the curly braces. I am ignorant to what they do but it seems they should ‘include’ the bottom portion of coding. So I guess the final question is: How do I get the array that I’ve returned into the correct/lower echo call of $value? Many thanks again. DFI can’t be sure that this is what you want because the code has changed too much since the first version, but give it a try:
<?php $mypages = get_pages( array( 'child_of' => 8, 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) ); foreach( $mypages as $page ) { ?> <h4><a href="<?php echo get_page_link($page); ?>"><?php echo $page->post_title; ?></a></h4> <div class="entry"> <?php $meta_values = get_post_meta($page->ID); $keys_to_show = array('phone_number', 'fax_number', 'regular_mail'); // array of keys to show foreach ($meta_values as $key=>$values) { if( in_array($key, $keys_to_show) ) { foreach ($values as $value) { echo "$value<br />"; } } } ?> </div> <?php } ?>
Just tried that, brilliant. The only odd thing is that the results of the array are displayed in a sort of random order. Meaning one store shows phone, fax, address while the next store shows phone, address, fax while the next one shows address, fax, phone. Just sort of odd. Any idea why? Otherwise this is exactly what I was looking for and cannot thank you enough for your help.
OK – replace this bit:
foreach ($meta_values as $key=>$values) { if( in_array($key, $keys_to_show) ) { foreach ($values as $value) { echo "$value<br />"; } } }
with this:
foreach ($keys_to_show as $key) { if( array_key_exists($key, $meta_values) ) { $values = $meta_values[$key]; foreach ($values as $value) { echo "$value"; } } }
This works beautifully, just added a line break in with value. May I ask how this works? More specifically why the first bit of code worked, but was random, and what changed in the second block to correct it?
The first code looped through the meta_values in whatever order they happened to be fetched from the database and listed ones that were found in the $keys_to_show array. The order of the meta_values was not forced to be the same for each post.
The second code loops through the $keys_to_show and listed them in that order, which was constant for every post.
So, if this problem is solved, please use the dropdown on the right to mark this thread ‘Resolved’.
For any who may follow. With much help, time and effort my issue has been solved. Original problem: List all pages (or specified ones) and all of the Custom Field values (or specified ones) for each page. How this was accomplished:
<?php $mypages = get_pages( array( 'child_of' => 8, 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) ); foreach( $mypages as $page ) { ?> <h5><a href="<?php echo get_page_link($page); ?>"><?php echo $page->post_title; ?></a></h5> <div class="entry"> <?php $meta_values = get_post_meta($page->ID); $keys_to_show = array('phone_number', 'fax_number', 'regular_mail'); // array of keys to show foreach ($keys_to_show as $key) { if( array_key_exists($key, $meta_values) ) { $values = $meta_values[$key]; foreach ($values as $value) { echo "$value<br />"; } } } ?> </div> <?php } ?>
The top portion of the code gets the list of pages using the get_pages() function. In this case, it is grabbing all pages that are a child of page ID ‘8’, and displaying them by post_title, and sorting them alphabetically by ‘ASC’. The proper link back to each page is set by using get_page_link($page), the $page variable being the result of the foreach loop result for each page in the list. We display each result with $page->post_title which is showing each $page result from the get_pages() function as the post_title.
The second portion of the code displays the Custom Field values for each page. We accomplished this by using get_post_meta($page->ID) to select the pages from the above function, then using the $keys_to_show array to select which Custom Field values to display.
Hope this may help somebody in the future, thanks to vtxyzzy who made this possible.
- The topic ‘All Custom Field Values AND Page Titles’ is closed to new replies.