WordPress.org

Ready to get started?Download WordPress

Forums

echo all custom fields (3 posts)

  1. prionnsias
    Member
    Posted 2 years ago #

    Hi everyone
    does anyone know the PHP code that would do this

    -----
    If the value of any custom field from the entire site = the title of the current page then output the name of the custom field as well as the title of the page it is from
    ----
    I am trying to build an IMDB basically for my production company

    so I James have a page with the title "James Murphy"

    On the film "Yakim" I was the producer.
    I have made a page called "Yakim" and there is a custom field named Producer, and the value of that custom field is James Murphy

    I want the "james Murphy" page to print out

    Producer - Yakim

    then i want it to search the rest of the site and output the same information from any page that it found my name

    Thanks everyone

  2. vtxyzzy
    Member
    Posted 2 years ago #

    I think the following code will do what you want if placed in the Loop for the current page:

    // Select all postmeta with a value matching current Page title
       $post_title = get_the_title();
       $sql = "SELECT m.*, p.*
       FROM $wpdb->postmeta m, $wpdb->posts p
       WHERE m.post_id = p.ID
       AND m.meta_value = '$post_title'
       AND p.post_type = 'page'
       AND p.post_status = 'publish'
       ORDER BY m.meta_key, p.post_title
       ";
       $results = $wpdb->get_results($sql);
       foreach ($results as $row) {
          echo "<p>$row->meta_key - $row->post_title </p>";
       }

    If you really meant to select Posts instead of Pages, change the post_type.

  3. prionnsias
    Member
    Posted 2 years ago #

    WOW!
    it worked!
    thank you so Much
    Now i just need to make it look pretty
    I really appreciate it!

Topic Closed

This topic has been closed to new replies.

About this Topic