WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] External file to get a custom field from post? (3 posts)

  1. DomenLo
    Member
    Posted 1 year ago #

    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!!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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
  3. DomenLo
    Member
    Posted 1 year ago #

    Wow, thank you so much!

    Will be testing this out ASAP!

Topic Closed

This topic has been closed to new replies.

About this Topic