Tag Info

Hot answers tagged

10

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

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

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

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

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


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

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


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

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


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


2

Okay there are a few things making this not work here. First, you need to pass strings to functions like get_post_meta. 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); Second, as you may have noticed above, the syntax for ...


2

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


2

but when I visit a month with no posts my template returns the date "January 1970" instead. Yes. That will happen. UNIXTIME began on Jan 1, 1970. That is "0000/00/00" but negative numbers work back until sometime in 1901. strtotime will return false for anything outside that range, including your nonexistent dates. date will assume "day zero" if given ...


2

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


2

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;


2

$post is never set on init when I test this-- function topSub( $Trunckvalue = null ) { global $post; var_dump($post); } add_action( 'init', 'topSub' ); The earliest hook I can find (by experimentation, not a canonical statement) is wp. Aditionally, $post is set on archive pages as well as "single" pages. On those archive pages it is set to the ...


2

As soon as you hit "Add New" and the new post pages starts to load, WordPress inserts a mostly blank post template into the database. Periodically after that you get auto-saves. You don't want either. You will need to add additional conditions to prevent your code from running in those circumstances. Something like this in addition to your ...


2

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


2

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


2

Change the line <?php echo apply_filters('the_content', get_post_meta($page->post_content, 'custom_tagline')); ?> to <?php echo apply_filters('the_content', get_post_meta($page->ID, 'custom_tagline')); ?> See http://codex.wordpress.org/Function_Reference/get_post_meta


2

That's because the fields weren't set then. Note: The Ā»AutosaveĀ« process/request also doesn't save them. Use the values from $_POST instead for your "new_to_publish" action.


2

You don't need to call wp_reset_query() here, because you're not modifying the main query. Instead, call wp_reset_postdata(). You only need to call wp_reset_query() when modifying the main loop via query_posts() (which you shouldn't do, anyway, so you should never need to call wp_reset_query()). But new WP_Query(), with a properly instantiated loop - i.e. ...



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