Skip to content
daggerhart edited this page Mar 16, 2012 · 1 revision

This example field outputs either the post_title or post_status.

<?php
// hook
add_filter('qw_fields', 'qw_field_example');

/*
 * My new field definition
 */
function qw_field_example($fields)
{
  // new field
  $fields['example_field'] = array(

    // title displayed to query-wrangler user
    'title' => 'Example: Field',

    // description on the field form
    'description' => 'Just a useful description of this field',

    // optional) callback for outputting a field, must return the results
    'output_callback' => 'qw_field_example_output',

    // (optional) where or not to pass $post and $field into the output_callback
    //    useful for custom functions
    'output_arguments' => true,

    // (optional) callback function for field forms
    'form_callback' => 'qw_field_example_form_callback',
  );
  return $fields;
}

/*
 * Example output callback with output_arguments = true
 *
 * @param $post  The WP $post object
 * @param $field This field's settings and values. Values stored in $field['values']
 */
function qw_field_example_output($post, $field){
  // adjust output according to my custom field settings
  if ($field['values']['my_setting'] == 'title'){
    $output = $post->post_title;
  }
  else if ($field['values']['my_setting'] == 'status'){
    $output = $post->post_status;
  }
  return $output;
}

/*
 * Provide a settings form for this field
 *
 * Output is expected of all forms, because they are executed within a buffer
 *
 * @param $field  - this field's settings and values
                    Values stored in $field['values']
 */
function qw_field_example_form_callback($field)
{
  // retrieve the value from the field for retaining settings values
  $value = $field['values']['my_setting'];
  ?>
  <select name="<?php print $field['form_prefix']; ?>[my_setting]">
    <option value="title" <?php if ($value == 'title'){ print 'selected="selected"';} ?>>Show Post Title</option>
    <option value="status" <?php if ($value == 'status'){ print 'selected="selected"';} ?>>Show Post Status</option>
  </select>
  <?php
}
Clone this wiki locally