How to add values in wp_postmeta? - php

I am working in wordpress.I have to add some fields to the my post and I want the added information of my post to be added in serailized form in metavalue in wp_postmeta.How can I do this?
I have found a page post.php that seem to handle this.But I just cannot seem to find anything that would let me add the values of my field to the postmeta.
HELP
Updated:
I have also seen add_post_meta Does this add the value into the database or just the adds the key and the value?

You need to add custom fields while adding new post to add new values in wp_postmeta.
Click on add new post and search for custom fields.

You can use the get_post_meta method to get a specific field or get_post_custom() to get them all.
But in this case i guess you ought to use the_meta(); that will get you the current post meta. once you add a custom field inside the post/page and put this in the template.
Learn how to use it Here

Related

Hide the whole metabox cmb2

i'm using the famous CMB2 to create custom metaboxes, i want to have one hidden field with default value which i will set myself. i used this hidden field type and its working fine , the thing is the metabox will have this field only, so i ended up with what looks like an empty metabox which may confuse the user. i have read dispaly options but this controls where should the metabox appear, i have it exactly where i want on my posts but i want to set it's visability to be hidden so i do not confuse the user , i thought about using css on this but i want to check if there is a way to achieve this using CMB API
i'm adding the css way answer because it works and achieves the requirement , but i would still like to know if there is another php way for educational purpose.
function admin_style() {
wp_enqueue_style('admin-styles', get_template_directory_uri().'/css/admin-css.css');
}
add_action('admin_enqueue_scripts', 'admin_style');

Custom input fields Wordpress back-end

I am working on a food-blog theme for Wordpress and I want the admin user to be able to add recipies in the back-end.
I'm thinking of a custom post format section similar to the regular post section where the user can add a title, the post itself, an excerpt etc.
I know how I can make such a section, but I want a custom field/row where the user adds an ingredient such as "water" and can add an amount and adds "grams"/"kilograms" etc. And do this for every ingredient.
I hope someone can help me with this, thanks!
Using Advanced custom fields
Documentation below
http://www.advancedcustomfields.com/resources/creating-a-field-group/
You can create fields that a user can insert data into and then you just parse that data into the the single page.
It is all documented nicely on the link above.

Get ACF from relationship field with Timber

I'm using Advanced Custom Fields on a custom post type. On the homepage I have a relationship field to select one of the posts in the custom post type.
Using Timber, I'm struggling to get the ACF fields for that relationship post into the Context. I can get the standard wordpress info like title and content, but not the ACFs.
I think I need to use TimberPost, but haven't had any success so far.
This is in my page template. In the view it just outputs all the standard WP stuff:
$context['featureRelationship'] = get_field('feature');
So I tried this, but I'm going wrong somewhere:
$context['featureRelationship'] = new TimberPost(get_field('feature'));
Appreciate any help, thanks
Sorted, so the php was right:
$context['featureRelationship'] = new TimberPost(get_field('feature'));
But I should have used a post object acf field instead of relationship

Wordpress - Don't show Advanced custom field meta in default Custom Fields dropdown

I'm using the plugin ACF which uses the wordpress meta data Or something similar to save the field name/content.
The problem I have is, I use the default wordpress Custom Fields for a few things, but once you've added a few fields through ACF it fills the list with generated content.
Which makes it hard for the client to find the fields that are used e.g tagline.
Is there anyway to filter what is output here?
Try using an underscore as a first character of your custom field name for ACF. Like _performances or _mycustomfield This would make the custom field hidden into the native custom field form in each post. For reference read it here.

Wordpress Edit Post Hook to load custom data

I have an meta_box in the post creation screen. As soon a user click on a specific category it will be displayed. For saving the information in the meta_box and post title and post content I use the hook post_updated. All is going to save fine in the database -- post data in wp_posts and the data in the meta_box into a special created table.
But sometimes the user want to edit this post from this 'special 'category. In this case the meta_box with the inserted information should be loaded and be editable. I know that I need to fill in the fields manually, but I need to know which hook is called when the user presses the edit link. An idea was, check for the url and if a parameter edit is found call my edit routine. But it looks like a dirty thing for me.
The codex is not very helpful. I tried the following hooks:
edit post, publish post, save post, wp insert post
But they are called after I pressed the actualize button.
BR,
mybecks
You don't have to use any hook for this, you just need to use following lines where you are generating your meta_box html
global $post;
now you have access the $post object when you are editing
get_post_meta($post_id, $key, $single);
use above function to get the value of particular meta and set it to your html as required.
for reading more about get_post_meta, goto http://codex.wordpress.org/Function_Reference/get_post_meta

Categories