• 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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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?

    Thread Starter bekar09

    (@bekar09)

    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 https://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.

    OK.

    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.

    Thread Starter bekar09

    (@bekar09)

    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.

    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.

    Thread Starter bekar09

    (@bekar09)

    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.

    Thread Starter bekar09

    (@bekar09)

    apljdi i’m waiting for your mail…

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘add custom field dynamically’ is closed to new replies.