Hot answers tagged

13

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


13

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


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


7

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


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

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


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

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


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


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

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


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

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

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


3

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

You got it quite right, but with a few minor problems. First, please use a uniqe prefix for your functions and values - opacity or meta_save are quite generic and could be used by other authors. Second, the part where you get the meta opacity was missing, I added it for you - just retrieve the meta value from the current post. Third, I then created a ...


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

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

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

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;


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

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


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


2

You can query your postmeta table directly using something like: global $wpdb; $metas = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta where meta_key = %s", 'add_your_key_here') ); echo '<pre>'; print_r( $metas ); echo '</pre>'; Implementation of custom fields postmeta table WPDB Class reference ...


2

I assume that you are talking about post meta-- data stored in the $wpdb->postmeta table when you talk about "meta data fields". The data in that table is associated with particular posts, of whatever type. It is not in any way keyed directly to the post type itself. There would be no way to connect a meta data field with a post type except by JOINing on ...


2

Sounds like a custom TinyMCE button would be the ideal candidate for the job. WordPress has an excellent write-up in the codex on how to create one; you'd essentially add a button to the TinyMCE toolbar with your own callback, which could trigger a modal pop-up to choose the audio file & insert the shortcode automatically. Equally, you could hook into ...


2

<?php $args = array( 'post_type' => 'attachment',***'meta_key'=>'your meta key','meta_value'=>'your meta value',*** 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ( $attachments as $attachment ) { echo apply_filters( 'the_title' , ...



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