Hot answers tagged

16

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 ...


15

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 ...


8

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( '<...


7

$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('...


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

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 ...


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

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(); ?>


4

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


4

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'...


4

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 ...


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

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( $...


4

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


4

If your posts page is a static page selected under Settings > Reading > Front page displays, then get_queried_object_id() will return the ID for that page which you can use to fetch meta data. $check_meta = get_post_meta( get_queried_object_id(), 'checkbox', true ); $header_meta = get_post_meta( get_queried_object_id(), 'header', true ); $...


3

Solution: <?php $object = new WPSEO_Frontend(); echo $object->metadesc( false ); ?>


3

You will want to make use of Yoast SEO's "WPSEO_Frontend" class. To display the meta description field content, you could do the following: <?php if (class_exists('WPSEO_Frontend')) { echo WPSEO_Frontend::metadesc(false); } ?>


3

If we look at get_permalink in source, we'll see the various filters applied to this function. For posts, it's post_link, for custom post types, it's post_type_link, for pages it's page_link, and attachments is attachment_link.


3

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 ); ?>


3

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 ).


3

The syntax for get_post_meta that you are using in incorrect. It is documented here. You need to pass a string for the second parameter. So, for example, use $icon = get_post_meta( $post->ID, 'apaitei_logariasmo_facebook', true ); Instead of $icon = get_post_meta( $post->ID, apaitei_logariasmo_facebook, true ); In addition, string literals ...


3

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 ( ! ...


3

I don't know of a Core function/method to retrieve post meta with the key. That isn't to say that there definitively isn't one. There may be. I don't know everything about WordPress, I just pretend to :) Or maybe it has just slipped my mind right now. However, the fourth parameter of update_post_meta is to ensure that you only update the value you want to ...


3

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] =...


3

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);


3

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 ...


3

As the comments on the question indicated, this was basically a pile of garbage (my words, not theirs). There are a number of typos and inverted boolean checks. It was kind of a nightmare. I managed to get the code working, and I don't like unanswered questions, so I wanted to work through the bugs from the top down and point out the solution that I ended ...


3

The value of the textarea must be printed between the opening and the closing tag: <form action="/"> <textarea name="whatever"><?php echo esc_textarea( $description ); ?></textarea> </form> Note the usage of the function esc_textarea() here. It prevents any possible character inside the variable $description from being ...


3

Because the credit data is saved in the attachment's post meta, not the main post: $credit = get_post_meta( $post->ID /* Wrong ID! */, 'credit', true ); Instead you need to catch the ID of the inserted image: function attach_image_credit( $images ) { $return = $images[0]; // Get the image ID from the unique class added by insert to editor: "...


3

This quite an expensive operation what you are doing, and IMHO wrong worksflow as well. What you want to do is, is the following Get all the meta_values from the specific meta_key you need. You can then check the random number against the values returned from a specific meta_key Here is a very basic idea in code: (Credit to Chinmoy Paul from ...



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