Categories
Package/plugin/module WordPress

Get Custom Field Values v3.0

I’d like to announce the official release of the updated Get Custom Field Values plugin (v3.0).

Use widgets or template tags to easily retrieve and control the display of any custom field values/meta data for posts or pages.

The plugin’s official homepage is located at :
coffee2code.com/wp-plugins/get-custom-field-values.

Highlights

This releases introduces a substantial number of features to this popular plugin. Probably of the greatest interest and request is that the plugin now provides a widget to allow access to all capabilities previously only available via template tags. In addition, there are even more capabilities, as four new template tags have been added (also accessible via the widget):

  • c2c_get_post_custom() : Useful when you know the ID of the post whose custom field value you want
  • c2c_get_random_custom() : Retrieve the value of a random instance of the specified custom field key, as long as the field is associated with a published posted, non-passworded post
  • c2c_get_random_post_custom() : Retrieve the value of a random instance of the specified custom field key for a given post
  • c2c_get_recent_custom() : Retrieves the most recent (according to the associated post’s publish date) value of the specified custom field

Newly added shortcode support allows you to embed custom field data inside the content of your posts, and a shortcode builder admin widget helps you construct the shortcode.

Note that this release drops support for versions of WordPress older than 2.6, that support has been tested through WordPress 2.8.1, and that the widget is only available under WordPress 2.8+.

Comments are welcome on this post for this version of the plugin. Comments will be closed once this release has been superseded by another.

Read on for a detailed ChangeLog of the release.

Detailed ChangeLog

These are the detailed changes, which may or may not make sense to you depending on your familiarity with the previous features and internals of the plugin.

  • Added widget support (widgetized the plugin)
  • Added shortcode support ([custom_field])
  • Added c2c_get_post_custom() : Useful when you know the ID of the post whose custom field value you want
  • Added c2c_get_random_custom() : Retrieve the value of a random instance of the specified custom field key, as long as the field is associated with a published posted, non-passworded post
  • Added c2c_get_random_post_custom() : Retrieve the value of a random instance of the specified custom field key for a given post
  • Added c2c_get_recent_custom() : Retrieves the most recent (according to the associated post’s publish date) value of the specified custom field
  • Used $wpdb->prepare() to safeguard queries
  • Dropped pre-WP2.6 compatibility
  • Changed description
  • Updated copyright date and plugin version to 3.0
  • Tested compatibility through WP 2.8.1

37 replies on “Get Custom Field Values v3.0”

I’m not sure how to install the new version over the old one. There’s some options.
I leave the old one and overwrite it with the new one and the 2 extra scripts.
I leave the old one and create the folder with the new 3 scripts.
I delete the old version and create the folder with the new 3 scripts (which seems most likely).
Most important question is therefore where do I put get-custom.shortcode.php and get-custom.widget.php ?

Please answer as I always enjoy the new features in an update..

Is your WordPress set up to allow you to install plugins via the WP Plugin Directory? If so, you can go to the Plugins admin page and click to upgrade the plugin automatically. If you want to do it manually, your best bet would be to delete the existing get-custom directory and then unzip the get-custom.zip file in your plugins directory. It’ll create a new get-custom directory and you’ll be good to go. Or you can replace the contents of your get-custom directory with the contents of the get-custom directory contained in the zip file (to include the shortcode and widget files, which need to be in the same directory as get-custom.php).

I did the automatic upgrade on one of my sites that uses this plugin *extensively* and now it’s throwing a weird error…

Fatal error: Call to a member function form() on a non-object in /…/html/wp-content/plugins/get-custom-field-values/get-custom.shortcode.php on line 105

Any ideas? I’d poke around further but I’m hip deep in some projects with 5p deadlines.

What version of WordPress are you running? 2.7.x perhaps? I may need to add an additional check, as the shortcode code (where the error is occurring) requires the widget code (which will only get activated in WP2.8). For a quick fix, you can comment out this line near the top of the get-custom.php file (which will effectively disable shortcode support):

include(dirname(__FILE__) . '/get-custom.shortcode.php');

But please do let me know if you are using WP2.8 or an earlier version. Thanks.

I’m using 2.7.1 still… I’ve been thinking about upgrading to 2.8, but have been chicken 🙂

Maybe that should be my project this weekend. I thought about doing it last weekend, but I didn’t want to spend the holiday fixing broken plugins if things went badly.

I can’t do the automatic upgrade but I do have ftp-facilities. The original get-custom.php is in my plugins directory without a directory of it’s own. If it already had a directory there would not be any problem. Do I just delete get-custom.php and create the directory you’re mentioning in the plugins-directory ?

Same error here. I’m using WP2.7.1
Is it possible to check for WP Version before using such stuff?
It is bad, if you get such an error after udating, very shocking ^^.

Smth. like that:

If (WPVers >= 2.8){
include(dirname(__FILE__) . ‘/get-custom.shortcode.php’);
}

I’ve released a new version of the plugin, v3.0.1, which includes a fix to prevent the error when running the plugin in a version of WordPress older than 2.8.

So to clarify: widget and shortcode support is only available for WordPress 2.8 and later, but all functionality from the pre-v3.0 version of the plugin is maintained, and the four new template tags also work in older WordPress blogs as well.

Blogman: Yeah, deactivate the plugin, then remove the get-custom.php file that is directly in the plugins directory, and unzip the plugin’s zip file in the plugins directory, which should create a directory called “get-custom” with all the php files in it.

I tried to install the file as well and am running the 2.8.1 and got this error when trying to install a fresh version of the plugin.

Fatal error: Cannot redeclare c2c_get_recent_custom() (previously declared in /nfs/c04/h02/mnt/61803/domains/mydomain.com/html/blog/wp-content/plugins/fresh-page/get-custom.php:320) in /nfs/c04/h02/mnt/61803/domains/mydomain.com/html/blog/wp-content/plugins/get-custom/get-custom.php on line 189

I just substituded mydomain where it initially had my actual domain name to keep it private.

Dee: It appears the Flutter (aka Fresh Page) plugin you have installed embeds a copy of some of the functions provided by this plugin, so there is a conflict since a function can only be defined once for a given name. That plugin should have renamed the functions it copied so as to avoid the conflict (or require users to separately obtain the Get Custom Field Values plugin). My version is more up to date, and it doesn’t appear as though they changed or added anything to them, so the fix for you would be to simply rename those functions in wp-content/plugins/fresh-page/get-custom.php. Look for function c2c_get_recent_custom and change it to function xxx_c2c_get_recent_custom and change function c2c__format_custom to function xxx_c2c__format_custom and you should be ok.

Thanks for a fantastic plugin!

I have a custom field on every post called “AuNa”.

I’m trying to use c2c_get_recent_custom to return a list of the past 10 AuNa values. I’d like these to be distinct, not duplicates.

Could you please help me out with the code, including any looping code I will need?

Thanks again!

By the sounds of it you are using the template tag rather than the widget? If so, did you try specifying the $unique argument (see docs)? An example:


<ul>
  <?php c2c_get_recent_custom("AuNa", "<li>", "</li>", "", "", "", 10, true); ?>
</ul>

Hi Scott –

Thank you for the plugin. I’m trying to find the shortcode creator and I don’t see it. Sorry for the ridiculous question, but I appreciate your help.

Thanks.

What version of WordPress are you using? The plugin’s widget and shortcode functionality only work for WP2.8 and above. If that’s what you have, you should see it at the bottom of the right sidebar when writing a post or page. If it’s not there, and you are using WP2.8 or later, do you see the widget in Appearance -> Widgets?

Hi Scott,

Thanks for the plugin, I’m trying to use it, but want to know if when inserting the code:

If it’s possible to make an “else”, so if this custom field doesn’t exist for the post, show a customized text. I don’t find the way to do it.

Thanks in advance.

Because of the version of mysql I’m stuck with for a particular site, I’m still using WP 2.0.2. I was wondering if you had a legacy version of this plug-in somewhere. If so, could you send me a link?

Thanks for this great plugin!

Hey, I tracked down version 2.1 in the wp plugins directory. One more question, I need to display only the custom field within the loop. I’ve been trying to make the custom field show-up instead of the body of the post, but haven’t had any luck. Do you have any words of advice?

Thanks!

Andrew Koop: Glad you found an older version of the plugin that worked for you. Showing the custom field in a loop instead of the body should just be a matter of modifying the appropriate template(s), replace either the_content() or the_excerpt() with a call to c2c_get_custom(). You’ll have to provide more details about what you’re trying to do if you need further assistance.

Hi Scott, sorry for the initial lack of details. The name of my custom field is “short-excerpt.” I’ve posted the relevant code from my page template below. Thanks again for your help! 🙂

<?php if ( have_posts() ) : ?>
<?php while (have_posts()) : the_post(); ?>
    <A NAME="<?php the_ID(); ?>"></A>
          <div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>      
        <div class="entry">
        <?php echo c2c_get_custom('short-excerpt'); ?>
        </div>
      </div>
    <?php endwhile; ?>
  <?php else : ?>
    <h2 class="center">Not Found</h2>
    <?php include (TEMPLATEPATH . '/searchform.php'); ?>
  <?php endif; ?>

This seems a very useful plugin
Is there a way for it to count the number of same values in a custom field and then display the number next to the value.
e.g a list of towns are the value and the number in brackets would indicate the number of posts from an area.

mytown (20)
yourtown (13)
anothertown (3)

Andrew: A quick glance suggests that the code you included should do what you originally requested: showing the custom field “short-excerpt” in place of a post’s content/excerpt. What are you experiencing? If you are still seeing the post’s body or other excerpt, then maybe you haven’t changed the appropriate template file? Pages generally use page.php, posts generally use index.php for the front page listing (or perhaps one of the others depending on what section you’re in. You could use my Reveal Template plugin to help figure out what template you should be editing.

mike: Currently the plugin does not provide such functionality. I’ve made note of it for possible inclusion in a future release. Offhand I’m not aware of a plugin that may help you out here (though one may well exist). Otherwise, it’d take a bit of custom code to make the appropriate database queries to generate the listing you want.

Hi

I made a test with a post that I let as a draft on my site. I dont’ want to publish it so that you can’t see it on my site.

In that post, I used many times your shortcode and it worked for all the custom fields except 1. I don’t know why. Is there a maximum of shortcodes to insert in a post ?
That is to say that in the test post, I have in the content : [custom_field field="Ville" this_post="1" between="" /]

In the post, I do have a custom field key Ville and also a custom field value. Do you have any idea why the value is not printed on the screen ?

The other shortcodes in that post have the same structure and they all work:
[custom_field field="Key" this_post="1" between="" /]

Thank you for your support

Raphael

Great plugin! Is there any way I could get my RSS feed to display the meta data generated by the custom fields input? My posts are exclusively meta-data (custom fields data) and hence the RSS feed comes up empty.

Thanks!

Raphael: There isn’t a limit on the number of shortcodes that can be inserted into a post. The example you cite should work the same as the others you’ve mentioned that do work. Is the custom field key name you are using, “Ville”, using accented characters in one place (as the actual key name for the custom field) or the other (in the shortcode)? I tried your example and it worked for me. Is there anything else unusual/different about the “Ville” custom field?

Javier: Yes, but it would take some customization via code, so how easy it can be done depends on how much coding you can do. A rudimentary example of it is (which can go in your theme’s functions.php file (after you’ve customized it to suit your situation, of course)):


add_filter('the_content', 'add_cfields_to_rss');
function add_cfields_to_rss( $content ) {
  if ( is_feed() ) :
    $content .= c2c_get_custom('event_name', "<p>Event name: ", "</p>\n");
    $content .= c2c_get_custom('event_location', "<p>Event location: ", "</p>\n");
  endif;
  return $content;
}

There are other checks you may want to do in the above code to ensure it only inserts custom fields for certain posts (i.e. posts with empty content, or in certain categories) (the above assumes all posts in the feed). You may also want to add more text, multiple custom fields, or other data, which are also all possible.

Hope that helps.

Hi Scott, great plugin. I was wondering if there’s a way to show custom field values from multiple pages in a single page. Thanks for the support

Alex: So you have a custom field by the name of ‘botox’ and you want its associated value? You’d do this:

$hej = "botox";
echo c2c_get_custom($hej);

Comments are closed.