Custom fields data disappears after a few mins?
-
Hello, I’ve created a custom post type, called ‘land_listing’ along with a custom field called ‘cost’. From my home page, I’ve got some code to display the land listings. Everything seems to work, but a few minutes after I create a new land listing in the WP admin screen, or if I click into different sections of the WP admin screen, the custom field (‘cost’) disappears from both the home page and from the ‘cost’ meta box in the WP post edit screen. Here’s the custom post type creation code in functions.php:
add_action('init', 'register_land_listing'); function register_land_listing() { $labels = array( 'name' => _x('Land Listings', 'post type general name'), 'singular_name' => _x('Land Listing', 'post type singular name'), 'add_new' => _x('Add New', 'Land Listing'), 'add_new_item' => __('Add New Land Listing'), 'edit_item' => __('Edit Land Listing'), 'new_item' => __('New Land Listing'), 'view_item' => __('View Land Listing'), 'search_items' => __('Search Land Listings'), 'not_found' => __('Nothing found'), 'not_found_in_trash' => __('Nothing found in Trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, // 'menu_icon' => get_stylesheet_directory_uri() . '/article16.png', 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => true, 'menu_position' => null, 'supports' => array('title','editor','thumbnail') ); register_post_type( 'land_listing' , $args ); } add_action("admin_init", "admin_init"); function admin_init(){ add_meta_box("cost-meta", "Land cost", "land_cost", "land_listing", "normal", "low"); } function land_cost(){ global $post; $custom = get_post_custom($post->ID); $land_cost = $custom["land_cost"][0]; echo '<label>For rent or free:</label> <input name="land_cost" value="' . $land_cost . '" />'; } add_action('save_post', 'save_details'); function save_details() { global $post; update_post_meta($post->ID, "land_cost", $_POST["land_cost"]); }
And here’s the code in my index.php file to display a list of the land listings:
query_posts( array( 'post_type' => array( 'land_listing' ), 'posts_per_page' => 10, 'post_status' => 'publish', ) ); while (have_posts()) : the_post(); the_title('<h3>', '</h3>'); the_content('Read the full post ?'); echo '<p>' . get_post_meta($post->ID, 'land_cost', true) . '</p>'; endwhile;
Thanks in advance for any help, I’ve spent hours on this without getting anywhere!!!
PS – I’m not sure where custom fields for custom post types are stored in the database? I tried to look into the DB with phpmyadmin to see if the data was saving probably, but no joy…
- The topic ‘Custom fields data disappears after a few mins?’ is closed to new replies.