More API for More Fields

Filed under tech

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.

Download More Fields API

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

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

4 Comments

  1. Jussi Jokinen
    Posted Wednesday October 14th, 2009 at 08:13 PM | Permalink

    Hi, your plugin is very useful, specially the extraction of image works like a charm. One question however: Can I somehow use menu_order attribute of pages to sort results in mfapi::get_posts? I installed this system to a web site and only later realised that pages should be sortable… Any ideas?

  2. Posted Thursday October 15th, 2009 at 11:26 AM | Permalink

    Hi Jussi.
    Happy you find this functions useful.
    You can try adding code to the query via the last parameter of mfapi::get_posts.
    Try with something like "ORDER BY menu_order".
    Let me now if it works!

    Regards

  3. Rich
    Posted Thursday October 29th, 2009 at 11:22 PM | Permalink

    This is an excellent plugin!
    What else can you use for the last parameter? I am looking for a way to order the posts by their timestamp

  4. Posted Thursday November 12th, 2009 at 10:18 PM | Permalink

    If you look at the code, the last parameter is just put in big SQL query. You need to have some confidence with SQL to use it…

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*