I have too much data stored in custom fields which I don't want to fetch on page-load, instead I'd give the user the opportunity to load the custom field values on click, possibly with ajax.

Is there a working way to do this?

I don't have any code yet just the wp loop. I'm thinking about putting the custom field queries in a separate php file, but I have no idea how to make connection between single.php and the separate file. (loading the file with ajax is no problem.)

UPDATE: I don't need ajax, it's just confusing me so far. I just want to display the custom fields associated with the post on a different page template. I think of it as if the custom fields would be "children" of a given post and clicking a link would load a page with just the custom fields on it.


  • 1
    You want to use the ajax capabilities provided by WordPress. Your question is a little broad. Take a look at the codex and let me know if anything specific is unclear.
    – v0idless
    Nov 4, 2011 at 13:43
  • Also, check out a very similar question I answered yesterday which gives you a basic intro to WordPress and AJAX. AJAX & WordPress Nov 4, 2011 at 14:23
  • Thanks. I'm afraid I don't quite understand this part of the codex... Updating my question.
    – elbatron
    Nov 5, 2011 at 13:34

1 Answer 1


I don't know if you've found an answer on this question yet since it's from 2011 but I have an idea on how to retrieve the data.

What if you created a VERY simple php file that took the post ID as a get variable and displayed the post's custom fields?

It would look something like this:

    require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
    $post_ID = $_GET['id'];

    $custom = get_post_custom($post_ID);

    foreach ($custom as $key => $value) {
        if (($key != '_edit_lock' && $key != '_edit_last'))
            echo '<tr><td>' . $key . '</td><td>' . $value[0] . '</td></tr>';

This will create a table of name (Custom Field Name) and value (Custom Field Value). If you don't want to use a table, of course you can format the internal bits however you want. I'm just trying to set you on the right track. Also, notice that there's an if statement in there. I'm sure you don't want to output the edit lock value or the last edit value.

Now, on to how to get this information from the page:

First, you'll need to create a button that will show the information.

<a href="<?php echo bloginfo('template_directory'); ?>/custom-field-provider.php?id=<?php echo $post->ID; ?>" class="show-more">See More Information</a>

And a box to show what you're going to AJAX in:

<div id="more-information-box" style="display: none;"></div>

You'll want to make up some cool little jQuery goodness here.

$(document).ready(function () {
    $('.show-all').on('click', function (e) {
        // Since we're already passing the data through the link, we don't need to do anything else. Just pull the url from the link.
        var url = $(this).attr('href');

        // A simple jQuery.get function to get the data from the php script we wrote earlier
        $.get(url, function (data) {

        // And stop the link from actually going to the page.
        return false;  // Better safe than sorry, right?

And that should do it for you.

I know it was a bit long-winded, but I really wanted to cover all the bases. I hope this helped you out.

  • Thanks @Alex, yes, I did something very similar although not that sophisticated. I'm in a middle of something right now and later today I your code a go.
    – elbatron
    May 17, 2012 at 15:53
  • You should use WordPress's built in AJAX handling rather than manually including wp-load.php.
    – Milo
    May 17, 2012 at 15:54

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.