WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to Exclude a certain Key-Value from the_meta()? I need the_meta_excluding (8 posts)

  1. Dgold
    Member
    Posted 5 years ago #

    Template tag, the_meta , has no parameters currently.
    http://codex.wordpress.org/Template_Tags/the_meta

    Can someone help make a plugin to add a parameter so that the_meta can EXCLUDE a particular Key-Value?

    It could be something like, php the_meta(exclude="Small-image")

    Or make a plugin with a new function like, php the_meta_excluding

    REASONING AND EXAMPLE:

    the_meta will display all the custom fields on a post. It will display them as Key - Value pairs. For example I could make my post display:

    In my example I want the_meta to show all the custom fields EXCEPT for "small-image" and "More private code that I do not want everyone to see".

    How to do this?

    I also submitted this in WP Ideas,
    http://wordpress.org/extend/ideas/topic.php?id=2012

  2. MichaelH
    Member
    Posted 5 years ago #

    Have you looked at get_post_custom_keys along with an 'if' test?

  3. Dgold
    Member
    Posted 5 years ago #

    Thanks for replying. get_post_custom_keys can be used for displaying just Keys (like, "My Mood"). I don't think it displays Key-Value pairs (like, "My Mood: Happy"). Plus, it doesn't have an "Exclude Key" parameter that I need, either. It just gets all the Keys for that post.

    That suggestion led me to get_post_custom ...

    That one can be used for getting Key-Value pairs. But it doesn't have an "Exclude Key" parameter either. Is there some way to use an 'if' test for this, MichaelH? I haven't been able to think of it yet.

    You know, if I could say which Custom Fields I will want to display (affirmatively, inclusively, knowing them in advance) then I could use get_post_custom_keys() and get_post_custom_values(). My problem is I can't say all the Fields I want to INCLUDE, but I know which ones I want to EXCLUDE. I want to display alllll my post meta (custom fields), that are now-known or may be added to any post in the future, EXCEPT for 1 or 2 custom fields that I know I want hidden.

  4. MichaelH
    Member
    Posted 5 years ago #

    Just stole some code from the_meta function to come up with this code you can use in your loop:

    if ( $keys = get_post_custom_keys() ) {
    echo "<ul class='post-meta'>\n";
    foreach ( (array) $keys as $key ) {
    $keyt = trim($key);
    if ( '_' == $keyt{0} || 'Small-image' == $keyt )
    continue;
    $values = array_map('trim', get_post_custom_values($key));
    $value = implode($values,', ');
    echo apply_filters('the_meta_key', "
    <li><span class='post-meta-key'>$key:</span> $value</li>
    \n", $key, $value);
    }
    echo "\n";
    }
  5. Dgold
    Member
    Posted 5 years ago #

    Yes! Power php code borrowing to the rescue. Thanks MichaelH.

    I held my breath and tested it, and it works.

    If you need to Exclude more custom field keys from the_meta, I simply add to this line with the || (which means "OR" in php)

    if ( '_' == $keyt{0} || 'autometa-plugin' == $keyt || 'runphp' == $keyt || 'Another Custom Field that I want to hide' == $keyt )

    In my example there, you may notice one of the reasons I needed this. Certain plugins, like runphp, and autometa plugin (which I am not recommending), use the Custom Fields. They write certain custom fields to each post automatically, then they process that info to display elsewhere. So I don't want them to be evident in my Post where I've got the_meta showing things like (examples) "My Mood" and "The Weather is".

    Thanks again MichaelH for attending to this solution. I would not have figured that out on my own. Big help for my site and my learning.

  6. stu-maru
    Member
    Posted 5 years ago #

    Fantastic!!!!!!!
    I have been pulling my hair out trying to figure out why on earth my Thumbnail Key and Value kept appearing on my single.php.
    This bit of code has removed them and others and has really helped me get out of a fix! Cheers!!

  7. scott-press
    Member
    Posted 5 years ago #

    I tried this on my page, and it shows one custom tag.

    I am trying to display all the users who have signed up and their profile information on a page for people who come to the site.

    I have been looking for days for a way to do this.

    Can anyone help?

    My issue is I used register plus to capture custom fields. for example

    in the wp_usermeta table I have key - value pairs like

    age - 50
    sex - Male
    golf_skill_level_ - Professional

    I want to make a profile display on the screen with those values...

    Anyhelp would be amazing !

  8. lordleiter
    Member
    Posted 5 years ago #

    One crucial thing to add to the bit of code above is to close out the UL tag like so:

    if ( $keys = get_post_custom_keys() ) {<br />
    echo "<ul class='post-meta'>\n";<br />
    foreach ( (array) $keys as $key ) {<br />
    $keyt = trim($key);<br />
    if ( '_' == $keyt{0} || 'Small-image' == $keyt )<br />
    continue;<br />
    $values = array_map('trim', get_post_custom_values($key));<br />
    $value = implode($values,', ');<br />
    echo apply_filters('the_meta_key', "</p>
    <li><span class='post-meta-key'>$key:</span> $value</li>
    <p>\n", $key, $value);<br />
    }<br />
    echo "</ul>\n";<br />
    }

    The big addition being the last line:
    echo "</ul>\n";

Topic Closed

This topic has been closed to new replies.

About this Topic