WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get "key" from "value" in custom meta (4 posts)

  1. mubojason
    Member
    Posted 3 years ago #

    i have a single post's custom meta, ex:
    custom meta key = moviename
    custom meta value = HarryPotter

    i can use code:
    <?php echo get_post_meta($post->ID, 'moviename', true); ?>
    to get the post's custom meta value "HarryPotter"

    but, how can i get the custom meta key "moviename",
    with the value "HarryPotter" that i know?

    please help me!

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    <?php
    $customkeys = $wpdb->get_results("SELECT meta_key FROM $wpdb->postmeta WHERE meta_value = 'HarryPotter'");
    
    $meta_key = $customkeys[0]->meta_key;
    ?>

    based on:
    http://codex.wordpress.org/Database_Description#Table:_wp_postmeta
    http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

  3. mubojason
    Member
    Posted 3 years ago #

    and more,
    if HarryPotter from a Variable like $name2,
    how to get it?

  4. mubojason
    Member
    Posted 3 years ago #

    ok, i get it!
    thanks for answer my first question~

Topic Closed

This topic has been closed to new replies.

About this Topic