Question

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

Scenario:

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.

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top