In a recent collaboration with Nicolò Volpato I used More Fields Worpdress plugin to implement a shopping cart.
More Fields is a lightweight plugin (compared to Flutter) that add custom fields and give the ability to create new type of contents. If you use it, you’ll find my plugin More Fields API very helpful.
I wrote this one-file plugin to give a better interface for theme developers. It’s coded in object-oriented paradigm, and requires PHP 5. Just copy the file in the plugins folder and activate it. You are free to use this code as you like.
Hide custom types from the Loop
The single most useful function here is mfAPI::activate_filter(). Just call it on top of your functions.php and you’ll filter out all the custom types from the usual Worpress flow.
This is especially handy if you derived a custom type from posts. In my case I created a type called “product”. Without this trick, I had products popping out everywhere on the blog.
In case you used Wordpress get_posts() function you should replace it with $wp_query->get_posts() because get_posts is not filtered.
If you use $wp_query->get_posts() a lot, sometimes you’ll notice Wordpress may get crazy results. To fix this call $wp_query->init() before get_posts.
Warning: if you call mfAPI:activate_filter() on top of your functions.php, don’t deactivate the plugin before removing this line of code
Get contents of a specified type
The second most useful function is mfAPI::get_posts(). Use it like this:
$products = mfApi::get_posts('product'); foreach($products as $product){ /* do something with $product */ };
This function has some awesome tricks. You’ll find every custom fields you added to this type as a property of the posts.
In my case the product type has a box with only a custom field, price.
So, I can print all the prices with this simple code:
$products = mfApi::get_posts('product'); foreach($products as $product){ echo $product->price; };
Another trick is the automatic extraction of the first image in the post. Just set the fourth param to true and you can do something like this:
$products = mfApi::get_posts('product',0,0,true); foreach($products as $product){ echo '<img src="' . $product->post_image . '"/>'; };
If you look at the other options, you’ll find you can easily use it to implement pagination.
/* * Return an array of posts of the specified type, with custom fields * * @param string $type custom field type * @param int $count max number of elements (0 for all) * @param int $offset start from this offset (user for pagination) * @param int $extract_image define if we should automatically extract an image * @param string $where optional mysql where */ public static function get_posts($type, $count = 0, $offset = 0, $extract_image = false, $where = '')
Other complex functions
- get_post() – get a single post (even inside the Loop) with all the benefits of get_posts()
- get_posts_by_tag() – filter custom content by type and tag
- get_similars() – return custom content sorted by tag similarity
Basic functions
- get_type() – retun the type of a content
- get_posts_count() – return the number of contents of a specified type (useful for pagination)
- get_custom_boxes() – return custom boxes for a specified type


