So I'm not sure what would be the best way to go about this in terms of best practices and optimization.


I have query that parses an external XML feed and stores the data using the transient API every 24 hours. This is stored in wp_options and the whole feed is stored in the option_value, one of the numerical values in the feed is for "weight". For example here is what the feed looks like for the this value.

<weight type="string"><![CDATA[120]]></weight>

The posts have a meta_box with a key called "meta_weight" with a numerical value.

The query compares these 2 values by grabbing the xml as an array and the meta value with something rather simple like: (I have simplified this example, it is working).

$xml_weight = $xml->weight;
$meta_weight = get_post_meta($post->ID, 'meta_weight', true);            

if $xml_weight > $meta_weight { echo "Your heavy";}

Now for the actual question:)

This value comparison is just done on the fly with php, I need to store it in the database using just false/true to tally all posts where this value is true.

Should I just add this value incrementally using $wpdb class and insert it into wp_postmeta.

Or is there some better method to just grab the value out of the DB and compare it to the meta box value for every post?

My sql knowledge is very poor.

  • Do I understand right that comparison result will need to be recalculated every time XML is updated (every 24 hours)?
    – Rarst
    Mar 20, 2011 at 16:57
  • Yes exactly, it does not have to be every 24 but at least every few days, that's why I think a direct sql query would be needed?
    – Wyck
    Mar 21, 2011 at 3:20

1 Answer 1


I would do the following:

  1. Create WP-Cron task (or just use daily wp_scheduled_delete one to tag along) and hook your function to it.
  2. In that function:

    • fetch XML file;
    • fetch all posts with meta_weight set, using get_posts();
    • loop through posts and save comparison result in another meta field for each.
  • Thanks I'm wondering how taxing this would be for hundreds of entries to loop through and get XML for each one, I guess there is only 1 way to find out:)
    – Wyck
    Mar 21, 2011 at 17:24
  • @Wyck I misunderstood slightly, I though there is one large XML from which values come. Be careful not to time out if you will fetch many files in cron task. Other way to spread XML fetches - you can couple comparison to post's page loads but that doesn't guarantee that all posts will have comparison filled and/or up-to-date.
    – Rarst
    Mar 21, 2011 at 18:18
  • Hmm ya each post is getting data from a separate XML file. Its actually the api.wordpress.com feed and for a site that I think will benefit the community and there was some interest in the irc dev channel about what I'm doing. I thought maybe another way to do this would be to create a custom RSS feed and parse it. Still not sure.
    – Wyck
    Mar 21, 2011 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.