WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] External file to get a custom field from post?

[Resolved] External file to get a custom field from post?

  • Hi everyone.

    I’m baffled by how to start something – I’d like to have a separate file (link.php) that could query the wordpress post table for a specific custom field/key. The file would be located among all other files.

    Say something like link.php?id=3&key=mycustomfield

    So that it would return that field value to me.

    Thanks!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Save the code below as link.php in your theme’s folder:

    <?php
    /* get custom field value for a post id */
    require('../../../wp-blog-header.php');
    get_header();
    $id = (intval($_GET['id'])) ? intval($_GET['id']) : 0;
    $key = (isset($_GET['key'])) ? urldecode($_GET['key']) : '';
    $value = get_post_meta($id,$key,true);
    echo "<p>ID:$id KEY:$key VALUE:$value</p>";
    get_footer();
    ?>

    Call it like this:

    http://yourdomain.com/wp-content/themes/yourtheme/link.php/?id=123&key=mycustomfield

    Wow, thank you so much!

    Will be testing this out ASAP!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] External file to get a custom field from post?’ is closed to new replies.
Skip to toolbar