What is the cleanest way to extract custom variables from a post
If you always need all of post meta at once this is the way to do it. To access post meta for specific key use get_post_meta() that will retrieve value(s) and unserialize if needed.
If you always need all of post meta at once this is the way to do it. To access post meta for specific key use get_post_meta() that will retrieve value(s) and unserialize if needed.
Couldn’t you do this through ACF settings page? You can select Post and set it equal to the title. Edit Updated image for clarification Edit 2 It turns out ACF does not query for custom type when display the title drop down. To include all post type, in /core/admin/meta_box_location.php, change <div rel=”post”> <?php $choices = … Read more
When you set the third parameter of get_post_meta() to false, it returns an array of all the values mapped to that key – setting this parameter to true returns a single value. So your code is correct. You’d get the exact same thing if you did something like this: $test = array( “1”, “2”, “3” … Read more
just add to your query SELECT DISTINCT $wpdb->posts. instead of SELECT $wpdb->posts.
try using get_metadata(‘post’, $postid, ‘teaser-text, true); these two actually same. should not make any difference. earlier i gave wrong arguments. var_dump() will show you what it is actually getting. please make sure, the ID is correct, ‘teaser-text’ exists. You can also try using some other meta name (for testing purpose only).
In your $args array, “meta_key” should be “event_time”. If you want to get events after a certain date you also have to set meta_value to the desired date and meta_compare to >, <, etc.. Note however that you’re not going to be able to order the posts with the format “June 6 2012 6:00 PM”, … Read more
building on keatch’s answer you only need to trim if its longer the 25 chars so: $trim_length = 25; //desired length of text to display $custom_field = ‘my-custom-field-name’; $value = get_post_meta($post->ID, $custom_field, true); if ($value) { if (strlen($value) > $trim_length) $value = rtrim(substr($value,0,$trim_length)) .'(…)’; echo $value; }
You can use the SQL way. Go in your database tool, like phpMyAdmin or Adminer (also possible via Plugin in the WordPress back-end). Use the SQL area for creating custom queries. Before you start, create a backup of the database and also a single backup for the table _postmeta. Change the string wpbeta_2 to your … Read more
You may want to hook somewhere in the gazillion hooks available in wp-includes/query.php. I would suggest something like this: function my_always_get_post_custom( $posts ) { for ( $i = 0; $i < count($posts); $i++ ) { $custom_fields = get_post_custom( $posts[$i]->ID ); $posts[$i]->custom_fields = $custom_fields; } return $posts; } add_filter( ‘the_posts’, ‘my_always_get_post_custom’ ); In this way you … Read more
You’re pretty close, but I wouldn’t be distracted by the ‘scope’ meta key. The following is untested. <?php $args = array( ‘post__not_in’ => array($donotrepeat), “post_type” => ‘event’, ‘meta_key’ => ‘_event_start_date’, // name of custom field ‘orderby’ => ‘meta_value’, “order” => ‘ASC’, “posts_per_page” => 1, ‘meta_query’ => array( array( ‘key’ => ‘_event_start_date’, ‘value’ => date(‘Y-m-d’), ‘compare’ … Read more