How can I get custom fields in wordpress?

Wordpress custom fields in custom post types

  • The theme I purchased came without custom fields in the post/product editor. It uses a custom post type in its own admin php file. I've managed to add custom fields by pulling the following code out of wordpress core metabox.php file however I'm unsure how to get it to work. Its missing the area where the custom field values should go. <div id="postcustomstuff"> <div id="ajax-response"></div> <?php $metadata = has_meta($post->ID); list_meta($metadata); meta_form(); ?> </div>

  • Answer:

    To get the custom fields associated to a post, you can query it this way: if ( get_post_meta($post->ID, 'my_customfield', true) ) : echo get_post_meta($post->ID, 'my_customfield', true) endif; Hope this helps

steen at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

The developer forgot to support "custom-fields" when calling register_post_type. When editing a post, if there is no checkbox under screen options for Custom Fields that's why. In the init hook for my plugin I do... register_post_type('mynamespace_product', array('labels' => array( 'name' => __( 'Products' ), 'singular_name' => __( 'Product' ) ), 'taxonomies' => array('category', 'product_type'), // this is IMPORTANT, 'public' => true, 'has_archive' => true, 'supports' => array('title','editor','custom-fields','comments') ) );

UprightNetizensBrigade

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.