Random Custom Fields

The latest version is 1.0, released February 24, 2009, compatible with WordPress v2.7 (and probably 2.6)

WordPress logoThis is an esoteric WordPress plugin (and widget) that will randomly extract one or more custom fields from your posts and display them in a format of your choosing.

For example if you’ve stored geographic coordinates (longitude and latitude) for your posts in a custom field, you can use this plugin to randomly generate a link to Google Maps. Perhaps you could even generate a random embedded Google Map centered on that spot. I know that some people use Custom Fields to store images with special meanings. They could use this plugin to randomly display an image linking back to the post it came from, similar to my Random Image Plugin.


This is not a sexy plugin, but it just might save the day. Here’s a screenshot of the admin interface.

Random Custom Fields screenshot


  1. Download and unzip random-custom-fields-1.0.zip (v1.0)
  2. Upload the file random-custom-fields.php to your plugins directory:


  3. Activate the plugin (don’t forget!)
  4. If your theme is widget-enabled, goto Design > Widgets and add “Random Custom Fields” to your sidebar
  5. If your theme is not widget-enabled, edit the index.php or sidebar.php template file in your theme’s directory:


    and add the following line of code where you want a random custom fields to appear:

    <?php random_custom_fields(); ?>

  6. Configure the plugin from within WordPress under Settings > Random Custom Fields


The <?php random_custom_fields(); ?> function can take 9 optional parameters in between its parentheses. If you specify any parameters this way, all settings made through the configuration interface will be ignored. Note: this behavior could be useful if you wanted to have several different random custom fields each with different settings appearing throughout your site.

<?php random_custom_fields(custom_field_name,
                           category_filter); ?>
  1. custom_field_name is a required string that determines which custom field name to display.
  2. include_posts is a boolean (default is true) that determines whether to pull custom fields from posts.
  3. include_pages is a boolean (default is false) that determines whether to pull custom fields from pages.
  4. sort_randomly is a boolean (default is true) that determines whether to pull custom fields from posts in random order, or in date descending order.
  5. count is an integer (default is 1) that determines how many custom fields to display.
  6. value_separator is a string (default is newline) that allows you to break up the custom field value into multiple pieces for use in the html_template.
  7. html_template is a string (default is %0) that determines how the plugin should display the custom field. There are several variables that the plugin can output by default, including %title, %permalink, and %excerpt, all from post of the custom field. The variable %0 will be replaced with the entire custom field value. If you’ve entered a custom value_separator (default is a newline) the variables %1, %2, %3, etc will be replaced with each piece of the custom field value divided by the value_separator delimiter.
  8. html_between_templates is a string (default is <br /><br />) that gets printed between custom fields if the count parameter is greater than 1.
  9. category_filter is a comma-separated list of category or tag ids (default is blank) that limits the custom fields posts within certain categories or tags.

Questions, comments, and suggestions are always welcome. If you’re interested in contributing to the code behind Random Custom Fields, it’s hosted on GitHub.

Feel free to if you found this useful.