Now easier than ever to use wp_query and sort posts using values from a field in Simple Fields

Today version 1.3.3 of Simple Fields is out. The full changelog is available at WordPress.org.

This update features a new and simplified way to sort posts using wp_query.

When using wp_query to fetch posts sorted by meta values you usually do something like this:

$args = array(
	"orderby" => "meta_value",
	"meta_key" => "my_custom_field_key",
	"order" => "asc"
);
$my_query = new WP_Query($args);

Then if you wanted to sort posts by a value that was stored by Simple Fields then you had to find out what the meta key for the field was and then use that in the query like this:

$args = array(
	"orderby" => "meta_value",
	"meta_key" => "_simple_fields_fieldGroupID_2_fieldID_0_numInSet_0",
	"order" => "asc"
);
$my_query = new WP_Query($args);

Pretty much the same, but a bit more difficult since you have to get the meta key and then you have to keep in mind that the meta key can change if you have a dev, stage and production server.

Here’s where the latest update come into play. Since we know the slugs for the field group and the field (we kinda entered them ourself in the GUI or using code, didn’t we?) we can use those slugs instead:

$args = array(
	"orderby" => "meta_value",
	"sf_meta_key" => "my_field_group_slug/my_field_slug",
	"order" => "asc"
);
$my_query = new WP_Query($args);

And boom! Simple Fields automatically gets the correct custom fields key and places that in the “meta_key” argument. No more worries about changing field ids between development and productions servers.

Altough it works great, you have yo remember that different field types store their values in different ways. it works really good for plain text, date/timepicker, and probably some more too, but there are field types that store their values as serialized objects and so on.

Oh, this update also adds the method get_field_by_fieldgroup_and_slug_string() that you can use yourself in your code if you for any reason want to get information about a field based on the slugs of the field and the field group is belongs to.

global $sf;
$field_array = $sf->get_field_by_fieldgroup_and_slug_string("my_fieldgroup_slug/my_field_slug");
echo "Here is some info about the field:";
print_r($field_array);

Import & Export your fields with the brand new 1.3 version

fields-export-and-import

The new import and export screen

Version 1.3 of your favorite WordPress Custom Fields Plugin is now here. Some of the highlights in this version:

  • The GUI in the settings now have tabs. Makes the GUI a bit less messy, and with it comes some filters and hooks that makes it possible for plugin to also add tabs there.
  • Import & Export: finally you can export and import fields. Currently you can choose what to export, and during import you can overwrite all your existing fields using that export. Pretty lame right now with only replace as an import option. But stay tuned, an import option to add fields that don’t already exist is comping in next version.
  • Using filters you now store fields by its slugs instead of ids. Very useful when ids have been messed up between development server and production server. Also useful when working with import and export, since ids are easily increased. I will try to post an intro/tutorial about this since it’a pretty cool and useful feature.

That’s it for now. Grab you update from wordpress.org or in your WordPress Plugins menu.

Oh, finally: if you like this plugin please give it a nice rating so other will know, and I will be a little bit happier. Thanks.

1.0.4 released with less cluttered fields and a long awaited add-link

Version 1.0.4 of Simple Fields is now live. It’s a small update that features two noticeable changes:

Less clutter & less space

This update features an updated edit post screen, with the label of each field now positioned to the left, makes each field group use up less space and also making the fields more scannable. I’ve tried this for a while myself and I really like it. Please post a comment and let me know what you think of this change!

Editing fields: Before on the left and after this update on the right.

Add link at the bottom of a field group

Yes, finally! Long awaited feature: when using repeatable fields, each field group now also has an “add link” at the bottom. Makes much sense when using Simple FIelds and it’s repeatable fields-feature for attachments and image galleries/slideshows. Before this update you could only add new fields to the top, making it really annoying and cumbersome when you wanted to add something to the end/bottom of the list.

Miss a feature in Simple Fields? Let me know about it!

Many of the new features that I add to Simple Fields are features that I’ve received as feature request from you users out there. So if you have a feature request (or a bug report!) please don’t hesitate to let me know by adding an issue over at Simple Fields GitHub page.