Site icon @Poremsky.com

Add a WordPress custom field and query on it

When I moved slipstick over to WordPress, one of my concerns was how to add articles to the front page and/or to the 'home page' of the different sections of the site. I don't want all new pages or all new pages within a specific category on any of my portal pages. There are times when I want to promote an older page to the front page, such as when an update is released that addresses a long standing-issue.

The guy who designed the site suggested using a "Featured" category, ordered by last modified date, on the pages I wanted on the home page. That worked Ok but there were a few things I didn't like:

  1. I had to assign two categories to some posts.
  2. When I added new categories, anything assigned to both Featured and the new category had a canonical url in Featured. (When I added new categories, I edited the database to change the Category ID of Featured to avoid this.)
  3. I needed to use the Stealth Update plugin to avoid updating the modified date on some pages and pushing them to the top of the portal. While I like this plugin and use it for other reasons, I had to remember to set it on pages I wouldn't normally use it with.
  4. In place of using stealth update, I could remember to remove the second category.

To avoid the category issues with posts on the section 'portals', I used tags instead. While this avoided some problems, the last modified date problems remained.

I wanted something that required less remembering. Custom Fields (custom meta data) to the rescue: adding a date field to the posts met my needs with little effort. I'm expecting it will be a lot less effort over time - the biggest issue right now is that the majority of the pages don't have the custom field and when I edit a post, it will add the field if it does not exist, setting the value to 'today'. As time goes on and all pages within my 'featured' tags have the custom date field, this won't be an issue.

I found a code sample I could use to tweak the function (I'm better at tweaking than writing php :)) to apply only to new posts or only when edited in the admin interface, but decided not to use it since my function adds the custom field only when a page is edited and only if the custom field does not exist. Eventually every post assigned to a 'featured' tag will have this custom date field. Problem solved. Passage of time and a little patience take care of it.

Query Posts

I'm using custom templates for the portal pages and previously used this query:

<?php query_posts('&tag=tag-name&showposts=13&orderby=modified'); ?>

Now I use this query, getting the meta key and values by tag.

<?php query_posts('&tag=tag-name&showposts=13&meta_key=featured-date&orderby=meta_value'); ?>

Date Function

I added this function to my function php page. It's not perfect because it uses the date I started a post, not the published date, as the field value. Since I often publish a post on date I start writing it, it's not that big of a deal (and it requires less code. :))

When I edit a page I don't have to remember to use stealth update, remove a category or tag. If I want the page back on a portal, I edit the custom field.

I use Ymd date format (yyyymmdd) so I can properly sort by descending values.

// adds created date to featured date meta

function save_featured_meta($post_id, $post) {

add_post_meta($post_id, 'featured-date',  date('Ymd'), true); 
    }

add_action('save_post', 'save_featured_meta',1,2); // save the custom fields

Now when I start a new post, a custom field called featured-date is added to the post and assigned today's date, in the format of yyyymmdd. If I want the post on a portal page, I add the proper tag and it's added to the portal. If I update the page with new information and want it back on the portal, I update the custom field by typing in the current date.

Exit mobile version