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


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


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


5

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


4

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


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


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

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


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

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 is more of a PHP question then a WordPress question. If you want to remove punctuation marks like (.,-;:) you can try this recursive version: $s = get_post_meta( $post->ID, 'one_line_summary', TRUE ); if( function_exists( 'remove_punctuation_marks' ) ) $s = remove_punctuation_marks( $s ); with /** * Remove punctuation marks (.,-;:) if ...


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

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

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

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

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


2

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'])) { ...


2

Also, if you can't get this to work with get_posts, I'd recommend switching to WP_Query. As Mridul said above, to sort based on numbers, you need to specify the 'meta_value_num' to 'orderby' parameter. However, I'm not certain that you can do this with a standard get_posts() call.


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



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