add custom field dynamically (8 posts)

  1. bekar09
    Posted 6 years ago #

    Hi everybody,

    i want to add custom fields to posts of a particular category dynamically using regular expression(which will find specific pattern from the post content and update the custom field).

    what should be the query or how should i proceed?

  2. s_ha_dum
    Posted 6 years ago #

    Something like this in the appropriate theme file-- index.php and or single.php usually but that can vary:

    //  not tested
    // could well be broken, stupid, or both
    if (mb_ereg("/regex/", $post->content)) {
      echo '<div class="custom_field">Stuff</div>';

    Getting the regex right should be the trickiest part. Also, where are you getting the content for the custom field?

  3. bekar09
    Posted 6 years ago #

    this is not what i'm looking for. i am inserting youtube videos through an automatic script and the post content looks like

    [youtube 11 digit video key]

    now the thumbnail for the video should be added in the form of custom field as in

    custom field name=thumbnail and custom field value=youtube thumbnail url

    now i have the youtube video key in the post content($video_key). for inserting the thumbnail url as the custom field value, what i want to do is a regex search from the post content and get the video key and thereby form the thumbnail url like http://img.youtube.com/vi/$video_key/default.jpg and insert into the wp_postmeta table.

    please suggest how to do it.

    anyways thanks apljdi for your reply.

  4. s_ha_dum
    Posted 6 years ago #


    1) If you have access to the script that is doing the inserting, you can edit it to use add_post_meta to insert your custom field key/value into the postmeta table.

    2) If you don't have access to the script, you might still be able to use the publish_post, save_post, or edit_post hooks to insert into the postmeta table. It depends on how your script is written.

    With #1 or #2 you might not have to do any regex at all, again depending on how your script works.

    3) I'm pretty sure you can use the method I described earlier to generate your thumbnail links, but #1 or #2 are better options as the regex would only run once (if at all) when you insert and not every time the page loads.

  5. bekar09
    Posted 6 years ago #

    i was using the add_post_meta function to insert thumbnails while posting new posts. but that caused a severe problem. when i access post-new.php for posting manually, then i see that all the custom fields for the video posts are already there. and it was taking pretty long to load the page and was getting hanged(www.clearglimpse.com/wp-includes/js/query/jquery.js?ver=1.2.6:11). i think there's some mistake in the script itself. so deleted all the custom fields from the wp_postmeta table and now all the thumbnails of the existing posts are gone.

    now i want to add the custom fields again for the existing posts and for new posts properly. your help is much appreciated.

    i'm really not getting you. can you drop me a test mail at bekar09 @ gmail .com? if you could kindly help me if you get some free time.

  6. s_ha_dum
    Posted 6 years ago #

    So you have a two part project. 1) Repair the database. 2) Get the script working properly in future.

    Yeah, there must have been something wrong with the script. add_post_meta works nicely in my experience. Its weird that your script caused post-new.php errors and even weirder that the page locked up on jquery because of it.

    I'll email you when I get a minute.

  7. bekar09
    Posted 6 years ago #

    thats absolutely correct. the second point is fixed to some extent by replacing add_post_meta with the update_post_meta function. but still i can see that once the script runs, it saves one custom field value in the post-new.php

    the most difficult part now for me is to Repair the DB.

    i'm eagerly waiting for your mail.

  8. bekar09
    Posted 6 years ago #

    apljdi i'm waiting for your mail...

Topic Closed

This topic has been closed to new replies.

About this Topic