Say I have a text file or JSON file elsewhere on my domain, that have a simple value, like "yes" or "no". I would like to update a custom field in a database table in Wordpress (postmeta) with the value of that file.

Posts or pages will have a section on them (divi) that will display depending on that value. If it's "yes", then display, if "no", then don't display.

Is there an easy way to do this? From outside WP: This is the preferred way for me, however, can I even do this? How secure is WP?.. I mean.. is it even possible to do this? (injecting the value into a WP table)

From inside WP: I know how to do a simple SQL UPDATE, and I read a little bit on $wpdb. But how do I only make the change if the value is different? (after all, no need to do an update every time a page loads...)

  • what are you trying to do that requires this? Where in the database it would be stored depends on how it would be used. Some form of polling would be needed, and the security depends a lot on the nature of the files contents, likewise the answer is very different depending on how you're displaying and using it. E.g. if it's the contents for a page, or a JSON file, etc, etc, can you edit more information into your question? And make sure you have a single well defined question
    – Tom J Nowell
    Commented May 15, 2023 at 17:19
  • @TomJNowell Thank you for your response. I've updated the question. I hope that it provides enough information this time.
    – Malachi
    Commented May 15, 2023 at 20:34
  • I see you mentioned that the area you're conditionally displaying is implemented using Divi, you can't ask about Divi and other 3rd party plugins here, the rest of your question is on-topic though. This JSON with the yes and no, is it a single yes and no for the entire site, or is it a yes for this post and a yes for that post and a no for those posts? If the file is on the same server then it's easier to open and read it then to mess around with the database
    – Tom J Nowell
    Commented May 16, 2023 at 8:38
  • @TomJNowell Sorry, had some time off, hence the delay in my response. I added the Divi stuff to paint a picture of what's going on, and it isn't really that relevant for my issue, but I thought I would it in there, since it may explain why I want to do what I want to do. As for the json, it's a single yes and no for the entire site. I understand that a global variable would make more sense, but due to the Divi ingredient, I'm stuck with custom fields per post. So yes, I would have to update ALL the posts that have that field in them.
    – Malachi
    Commented May 22, 2023 at 19:24
  • if it's a single sitewide value then an option makes more sense than post meta, but it would be much much easier to just read the file, why involve the database? Then you got to write code that checks the file regularly to update the database which just makes everything slower and weirder,
    – Tom J Nowell
    Commented May 23, 2023 at 9:18

2 Answers 2


you can read your file using standard PHP:

$value = json_decode( file_get_contents( 'path/to/your/file.json' ) );

Then you can use WordPress function to update post meta:

update_post_meta( $post_id, $meta_name, $value, $prev_value );

$post_id = ID of the post you want to update $meta_name = name of the meta field you want to update $value = new value $prev_value = only posts with this value will be updated (so if you want to only switch yes to no, you can put "yes" there.

If you want to use SQL:

global $wpdb;
    $wpdb->prepare( 'UPDATE ' $wpdb->prefix . 'postmeta ON meta_value = %s WHERE post_id IN (%s);', $value, join( ',', $post_ids ) )

$post_ids = array of all post IDs that should be updated $value = your new value

  • 1
    note that using raw SQL will not run filters hooks or update/flush caches, depending on the way the site is configured you might see weirdness, e.g. code to update a CDN when a page changes would be unaware you changed the DB directly, the same with caching plugins
    – Tom J Nowell
    Commented May 16, 2023 at 8:36
  • @TomJNowell Thank you. I just tested that, and yes.. when I run some raw SQL, it won't update caches... so I'll have to do it using update_post_meta then. Is there a simple way to run wp_query to just get an array of posts id's where the value of field xx is Yes (or No)?
    – Malachi
    Commented May 22, 2023 at 23:51
  • you can request just the post IDs from WP_Query but it means that WP won't prefetch values in bulk to optimise, so if you do anything it will trigger lots of tiny SQL queries which might be slower. E.g. when you do a normal WP_Query it fetches the posts row, but it also fetches all its meta and all its terms at the same time to reduce the number of queries and round trips. None of that happens if you just fetch the IDs
    – Tom J Nowell
    Commented May 23, 2023 at 9:15
  • Additionally, if you have a $post_ids list in a file, why would you need to use WP_Query to fetch posts? Just grab the IDs from the file, and call update_post_meta with them
    – Tom J Nowell
    Commented May 23, 2023 at 9:16

Do something like this (untested):

function should_it_show() : bool {
    $yes_or_no = json_decode( file_get_contents( 'path/to/your/file.json' ) );
    if ( $yes_or_no === 'yes' ) {
        return true;
    return false;
  • you said it's a single value for the entire site so post meta doesn't make sense
  • an option makes more sense, but that means watching the file and checking it regularly to update that option, that's slow!
  • the fastest most efficient way to figure out if you should or should not show, is to read the file and parse the value, the database is a distraction

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.