Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
29

Add the following code in the archive template loop to display Yoast SEO meta description for each post. echo get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true);


22

To answer this, I have gone and done some tests on this, and the results was actually mind blowing. Here is my test To this yourself, set yourself up with a test page. Just simply copy page.php, rename it and delete the loop. Now just create a new page in the back end. Before you start, first test your timer with empty info to get the amount of queries ...


16

Each post meta key can have multiple values. So you get the values by default as an array; the first entry (0) is the oldest. get_post_custom() fetches all meta keys with their values. You could simplify your code with: $price = get_post_meta( $post->ID, 'price', TRUE ); $comake = get_post_meta( $post->ID, 'coMake', TRUE ); When the last parameter ...


16

WP API has a rest_prepare_post filter (or rest_prepare_CPT if you are working with custom posts) which you can use to modify the JSON response. In your case it will be rest_prepare_joblisting. function filter_joblisting_json( $data, $post, $context ) { $phone = get_post_meta( $post->ID, '_phone', true ); if( $phone ) { $data->data['phone'] = $...


15

Get Archive SEO titles If you defined a Custom Post Type archive title you can get that by: $titles = get_option( 'wpseo_titles' ); $title = $titles['title-ptarchive-POST_TYPE']; echo apply_filters( 'the_title', $title ); Remember to replace POST_TYPE by your own Custom Post Type. To display all the wpseo_title variables, you can use: printf( '<pre&...


11

In general, storing PHP serialized data in database in a bad idea. It can be easily understood if you use multple key-value pairs of data in one field row, that is, you use an array or a object with one meta key. Imaging a car as the object. You can set multiple meta values to describe the car, for example color and fuel. You can serialize the data and ...


8

You could use the empty function inside your if as such : <?php if( empty( get_post_meta( $post->ID, 'price_list_category1', true ) ) ) : ?>style="display:none;"<?php endif; ?> The above returns an error, you should assign the return value to a variable. See my edit below. Warning empty might not be the best option depending on the values ...


8

To show post type meta data on a single page template, I assume that you're in the Loop. // Use get_the_ID() to get the ID via the API function echo get_post_meta( get_the_ID(), 'my-info', true ); // You can also call it from the global, as the query refers to the current single page echo get_post_meta( $GLOBALS['post']->ID, 'my-info', true ); If you're ...


8

$post->ID is what makes the meta value distinguished across all posts with the same meta key. So if you want to shorthand the get_post_meta call for the current post you can do this: function get_cuurent_post_meta($key){ global $post; return get_post_meta($post->ID,$key,true); } and you can call it like this: echo get_cuurent_post_meta('...


8

You should use get_post_meta() twice (Ref to your other question): <?php echo get_post_meta( get_the_ID(), 'club', true ); echo '<br />'; echo get_post_meta( get_the_ID(), 'date', true ); ?>


8

After a lot of messing around with this, I think I found a fairly good solution here. I realize this is over a year after you asked but this was bothering me and I couldn't find a good solution until now. The problem is that the get_post_metadata function doesn't allow you to access the current value. This means you aren't able to transform the value, just ...


7

This gets asked a lot so lets try and fully explain it. We can simply wrap it in an if statement and echo the value, for example, <?php if ( get_post_meta($post->ID, 'genre', true) ) : ?> <?php echo get_post_meta($post->ID, 'genre, true) ?> ?> <?php endif; ?> But that is ugly, and why do 2 queries when you can do ...


7

Just add an underscore before the metabox ID so the part of your code where you add a metabox looks something like this: $id = '_drama-description'; $title = 'Description'; $callback = 'drama_description_metabox_markup'; $screen = $post_type; $context = 'normal'; $priority = 'high'; $callback_args = 'null'; ...


6

Try meta_value_num 'meta_key' => 'metadata1', 'orderby' => 'meta_value_num' Add these 2 parameters to $args. This does the sorting considering metadata1 as numeric


5

You can use get_post for that Example: <?php $post = get_post($id); //assuming $id has been initialized setup_postdata($post); // display the post here the_title(); the_excerpt(); the_post_thumbnail(); wp_reset_postdata(); ?>


5

<?php if ( get_post_meta($post->ID, 'floorplans', true) ) : ?> Your code <?php endif; ?> Should do it... I believe


5

On archive page in post loop add following line of code to make it work echo get_post_meta(get_the_ID(), '_yoast_wpseo_title', true); Tell me whether it is working for you or i will provide another solution.


5

This function worked for me: function get_complete_meta( $post_id, $meta_key ) { global $wpdb; $mid = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key) ); if( $mid != '' ) return $mid; return false; } it will return an array of objects like: Array ( [0] =&...


5

Just had the same problem and, using your code above, here is how I solved it: function getqtlangcustomfieldvalue($metadata, $object_id, $meta_key, $single) { $fieldtitle="fields_titles"; if($meta_key==$fieldtitle&& isset($meta_key)) { //use $wpdb to get the value global $wpdb; $value = $wpdb->get_var( "SELECT ...


5

$post in the callback function is an array, not an object. So you cannot use $post->id. Change it to $post['id'] and it should work: function slug_get_phone_number($post, $field_name, $request) { return get_post_meta($post['id'], '_phone', true); } I recommend to change _phone to phone_number or something else without underscore prefix. Because _ is ...


4

You can use metadata_exists(); (worked for me)for checking for any post meta and the do whatever you want. // Check and get a post meta if ( metadata_exists( 'post', $post_id, '_meta_key' ) ) { $meta_value = get_post_meta( $post_id, '_meta_key', true ); }


4

I found this via searching for a solution myself, but it dawned on me the answer is very simple. You simply need to check if the value is empty, if it is then echo nothing - if it has content, then display the content - the code i used is below and can be tailored accordingly to anyone who needs to use it. <?php $meta = get_post_meta( get_the_ID(), 'page-...


4

You can use get_post_custom() which will return an array of all post meta in one single call to the database, that should save a few calls. And as for including the file over and over you could use get_template_part() to include a file once which will hold the entire loop (a good example can be found in the default twenty ten or eleven theme).


4

If every meta value has the key f_name $meta = get_post_meta($post->ID, "company_wp_box_g", true); echo $meta['fname']; If you're not sure of the contents & want to search for the first one available $name = false; $meta = get_post_meta($post->ID, "company_wp_box_g", false); foreach($meta as $array) { if(isset($array['f_name'])) { $...


4

I suspect you just want to see the content of the array for dev purposes, in which case s_ha_dum's suggestions of print_r( $mpdata ) or var_dump( $mpdata ) should suffice. To output the content of the array as a string (assuming it is not multi-dimensional) you could just echo implode( ', ', $mpdata ).


4

Try this inside save_post but please note the code is not tested $old = get_post_meta($post_id, 'products'); $new = isset ( $_POST['products'] ) ? $_POST['products'] : array(); if ( empty ($new) ) { // no products selected: completely delete alla meta values for the post delete_post_meta($post_id, 'products'); } else { $already = array(); if ( ! ...


4

Your meta field contains the ID of the attachment, you need to fetch that ID, then get the filename using that attachment ID. $doc_id = get_post_meta( get_the_ID(), 'pdf', true ); $filename = basename( get_attached_file( $doc_id ) ); echo $filename;


4

background is not a Core meta field. Since you say that get_post_meta( '22', 'background', true ); returns an image ID, I can only assume that whatever saved that value saved the image ID and not the filename. You can convert the filename to an URL with wp_get_attachment_url() or get other information including a partial file name with ...


4

All of these cost another DB Query (each!), and worse still since they are in a loop each one happens around 300 times. Don't panic! The posts from your query are stored in WordPress' object cache (which is simply memory, unless you have a custom cache system in place). All functions that operate on posts route through this cache, so in your case there's ...


4

This is known as array dereferencing and is only available in PHP 5.4+ To support older versions, you need to assign the array, and then access the index: $data = get_post_meta( $product_id, '_product_image_gallery' ); $gallery_pictures_id = $data[0]; Having said that, in your case use the third argument "single": $gallery_pictures_id = get_post_meta( $...


Only top voted, non community-wiki answers of a minimum length are eligible