Using Meta Box in wordpress

How to use Meta Box in wordpress so that items or articles are ordered by date and time.

Meta Box is a useful plugin in WordPress that can be used directly from the backend and also directly from the theme and gives the programmer the possibility to add other fields for any item when want to build a custom post type. While we were working on one of the websites that we recently finished http://fidelityrp.com a real estate website for Fidelity Rental Properties, LLC we chose to use the Meta Box to add additional fields in the slide show, such as rental price, address, location, no. of rooms and bathrooms. Also during the website building process we had to insert a field for properties that had the currently rented status to establish the date when they would be available and for this we used the same technique. What Meta Box does is that it gives the programmer, the option to add a unique field for the date, but since the problem is that such input is recorded in the database in text format then such a field needs that its items are ordered according to the date in an increasing fashion from the nearest date onwards. In PHP and other programming languages this kind of ordering is done only with the use of numbers and to do this the date field must be registered in the database as UNIX timestamp the format YYYYMMDD so the date Sat, 03 May 2014 06:42:05 GMT will be converted in the database as 1399099325.

To do this one must add a custom Meta Box for the specific article/item, which it is done in the file config-meta-boxes.php and which is found in the plug-in directory if this action is done directly in the Theme. If not, then this can be added from the backend of WordPress and the format of Meta Box must be datetime which can be used as shown below:

$prefix = 'SHOM_TEK_';
global $meta_boxes;
$meta_boxes = array();

$meta_boxes[] = array(
// article ID this can be different on you based on your custom post type   
 'id' => 'property_details',
// Meta box title - Will appear at the drag and drop handle bar. Required.
'title' => __('Property Details','framework'),
// Post types, accept custom post types as well - DEFAULT is array('post'). Optional.
'pages' => array( 'property' ),
// Where the meta box appear: normal (default), advanced, side. Optional.
'context' => 'normal',
// Order of meta box: high (default), low. Optional.
'priority' => 'high',
// List of meta fields
'fields' => array(
// Dow here you will put all the custom meta boxes for your custom post type
array(
'id'        => "{$prefix}available_date",
            'name'      => __('Available Date','framework'),
            'desc'      => __('If property status is Soon For Rent provide the Available date','framework'),
            'type'      => 'datetime',
        ),

Now at your custom post type you will have a custom field which is a date time field.

This field will be registered in the database as text format. To change it in the UNIX timestamp format go to fie datetime.php which is in the folder inside the Meta Box folder and near line 93 the following must be changed:

From:
static function normalize_field( $field ){
	$field = wp_parse_args( $field, array(
	'size'       => 30,
	'js_options' => array(),
	'timestamp'  => false,
	) );
To:
static function normalize_field( $field ){
	$field = wp_parse_args( $field, array(
	'size'       => 30,
	'js_options' => array(),
	'timestamp'  => true,
	) );

Now, every time the date field needs to change it needs to be done in the UNIX timestamp format.

To show the date in frontview in your post the Meta Box needs to be called as follows:

ID);
		//get the date
		if( !empty($post_meta_data['SHOM_TEK_available_date'][0]) ) {
			$av_date = $post_meta_data['SHOM_TEK_available_date'][0];
			echo ' Available After ';
// convert to Unix timestamp, then reformat, then print result
			echo date("M d Y", $av_date);
			echo '';
		}
	?>

This will convert the date from the UNIX timestamp format 1399099325 to May 3 2014.

To order the articles/items according to the date a similar format needs to be used and of course this depends on the manner and method how the template is coded:

 'your_custom_post_type',
         'posts_per_page' => $number_of_posts,
         'paged' => $paged,
         meta_query' => array(
         array(
          'meta_key' => 'SHOM_TEK_available_date',
          'orderby' => 'meta_value_num',
          'order' => 'ASC'
         );
      ?>

This model will order and list all articles/items according to the nearest date.

If detailed search is needed and it is required that posts are ordered according to the date, then in the search function something like below needs to be implemented:

if($_GET['status'] === 'soon-for-rent' ){
           		$search_term['orderby'] = 'meta_value_num';
           		$search_term['meta_key'] = 'SHOM_TEK_available_date';
         		$search_term['order'] = 'ASC';
        	}

Be careful: status, soon-for-rent, SHOM TEK_availabe_date and search_term are terms used to be executed in the template built for http://fidelityrp.com, where status is a variable in WordPress to call the status of a property, soon-for-rent is the content of a custom taxonomy for the template, SHOM_TEK_available_date is the ID of the Meta Box created for the datetime whereas the search_term is a variable we created for the search function.

Exit mobile version