• Is it possible to set a php function saying: if an option of a selector (1) is saved , so print the tag “name of the tag” in the tags box ?

    (1) this selector acts as a meta box in the post edit page

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 22 total)
  • Moderator bcworkz

    (@bcworkz)

    Not exactly, but what is possible would be to have the meta box save function also assign specific tags to the post. Then when the editor reloads, the assigned tags will be displayed in the tags meta box.

    For example, let’s say you selected the option “foo” in a post meta box. When you save the post, “foo” might be saved under the meta key “function_bar”. While that is being done, the function could also assign tag “foo” to the post. When the editor is reloaded, the meta box will not only display “foo” as selected, but “foo” will also be listed in the tags meta box.

    Thread Starter sacconi

    (@sacconi)

    Ok, let’s imagine the field is the following one: https://pastebin.com/dMHQ4PJ3

    where and what I should insert to have a specific tag printed in my tags box? Only if I use the selector, if it’s in the “0” position, no tag

    Moderator bcworkz

    (@bcworkz)

    In your save_metadata() function, you can assign tags to the post with wp_set_post_tags(). After doing so, the tags you set here will appear in the editor when it reloads.

    I’m not quite sure what your intent is, but perhaps add this:

    if ( ''!= $_REQUEST['piano']) {
       wp_set_post_tags( $postid, sanitize_text_field( $_REQUEST['piano']), true );
    }

    What I’m unsure of is if you save the floor location in post meta, why also save it as a tag? I would think one or the other would be adequate. One of my basic rules is to avoid redundant data. A personal preference. You may have redundant data if you want. I just don’t think it’s necessary.

    Thread Starter sacconi

    (@sacconi)

    Now I’ll explain my need. I may need a selector to indicate the type of parking space. It can be a “garage”, an “uncovered parking space”, a “shared parking space” or a “private and numbered parking space”, or a “parking space 300 m away from the apartment”. So I have a lot of options. However, all these options must correspond to only one tag, with the name “parking space”. Because in my search box there will be the possibility to filter the search results also based on tags. Each tag will have a check box. Clearly I can’t have hundreds of check boxes. For this I need a tag that summarizes multiple selections. This tag can also be two words and no hyphens in between. It would be useful if the tag name was not exactly the same as the meta key of the custom field. Is there a solution for this? The aim is that when I insert a type of parking space I don’t have to waste time inserting a tag, because the tag creates itself

    • This reply was modified 2 years, 3 months ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    So no matter what parking place meta data is selected, if any parking space is available of any type, a “parking” tag is assigned? Or only a few tags for many meta variations? A many to few relationship IoW.

    There is no relation between tag names and meta keys, so they could be the same if you wanted to. I think it’d be confusing, but it’s possible.

    The “auto-tagging” feature is possible with some variation of what I suggested in my last reply. Logic similar to: selection case 1-5, set tag A; selection case 6-10, set tag B, etc….

    Tag labels can be multiple words with spaces, but the tag slug must follow post slug rules: all lowercase, no spaces, no diacritics, hyphen the only allowed punctuation.

    A reminder: the tag taxonomy name is actually “post_tag”. May or may not be important.

    Thread Starter sacconi

    (@sacconi)

    | So no matter what parking place meta data is selected, if any parking space is available of any type, a “parking” tag is assigned?

    yes

    But the tag name in italian would be “posto auto”, I cant write “posto_auto”, because the tag name will be used in the search box to filter the results. Is this possible?

    I dont need different tags for different group of selections

    Moderator bcworkz

    (@bcworkz)

    “posto auto” is fine for the tag name. The tag’s slug needs to be “posto-auto”. The slug is not shown to users unless they know where to look. They only see the name. When you use wp_set_post_tags(), you specify the tag name. The slug is automatically generated, you don’t get a choice.

    In the option list, the option value attribute should be the slug, but the option label should be the name:

    <select name="tag">
      <option value="posto-auto">Posto Auto</option>
      <!-- more options... -->
    </select>
    Thread Starter sacconi

    (@sacconi)

    This is my field with the meta key “postoauto” https://pastebin.com/9XH4vbBb

    Should I put this

    if ( ''!= $_REQUEST['postoauto']) {
       wp_set_post_tags( $postid, sanitize_text_field( $_REQUEST['postoauto']), true );
    }

    in my function save_metadata_postoauto($postid) ?

    where I define the name of the tag?

    name = “posto auto”, slug= “posto-auto”

    Moderator bcworkz

    (@bcworkz)

    That sounds about right. Try it and see what happens 🙂

    Please confirm that the field or key name the meta value is posted with is “postoauto”. This code may not do as you intend though. It will assign (and create if necessary) a tag that’s the same as the saved meta value. If you save “garage”, a “garage” tag would be created and assigned. If the tag should always be “posto auto” regardless of what parking meta value is saved, change the sanitize_text_field( $_REQUEST['postoauto']) part to 'posto auto'.

    The true value may not be as you intend either. It will add the tag to any other tags already in place. If you want it to be the only tag set, possibly removing other tags already set, use false instead.

    Thread Starter sacconi

    (@sacconi)

    I tryed the following but it has not effect, I cant see any new tag in the tag box

    function save_metadata_postoauto($postid)
    {   
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
        if ( !current_user_can( 'edit_page', $postid ) ) return false;
        if( empty($postid) ) return false;
    
    
        if ( is_null($_REQUEST["postoauto"]) ) {
            delete_post_meta($postid, 'postoauto');
        } else {
            update_post_meta($postid, 'postoauto', $_REQUEST['postoauto']);
        }
    
    if ( ''!= $_REQUEST['postoauto']) {
       wp_set_post_tags( $postid, sanitize_text_field( $_REQUEST['posto auto']), true );
    }
    
    }
    
    Moderator bcworkz

    (@bcworkz)

    What action is that function hooked to? “save_post”? Does the post_meta part work?

    Why two different key names? $_REQUEST[‘postoauto’] vs. $_REQUEST[‘posto auto’]
    Is the tag supposed to be the same as the meta value or should it always be “post auto”? If always “post auto” change the sanitize_text_field( $_REQUEST['postoauto']) part to 'posto auto'.

    Thread Starter sacconi

    (@sacconi)

    The custom field is working correctly, it saves the values, here is the complete code: https://pastebin.com/8RaiMuWs , meta key is “postoauto”, tag name = “posto auto”, tag slug= “posto-auto” , “true” or “false”?, maybe better “true”, after the creation of the new tag I’ll delete the old one with the same name (they will have 2 different ID)

    • This reply was modified 2 years, 3 months ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    Using “false” would save you the trouble of manually deleting the old one, but you would also risk removing other tags you wanted to keep. “true” is the safer choice.

    Thread Starter sacconi

    (@sacconi)

    The custom field is working but it doesn add any tag. Meta key is “postoauto” and tag is “posto auto”. What I have to change? I suppose I cant use “posto auto” as meta key because no empty space is allowed

    I tryed also

    if ( ''!= $_REQUEST['posto auto']) {
       wp_set_post_tags( $postid, sanitize_text_field( $_REQUEST['posto auto']), true );

    Probably it has no sense anyway, if I have to write the meta key after $_REQUEST

    Moderator bcworkz

    (@bcworkz)

    Use the form field’s name attribute here:
    if ( ''!= $_REQUEST['posto auto']) {
    I assume it’s “postauto”, not “posto auto”. What the meta key, meta value, or tag name do not matter here, only the field’s name attribute matters.

    If the tag you assign will always be “posto auto” regardless of term meta value (spaces are allowed in term names, not slugs), do this:
    wp_set_post_tags( $postid, 'posto auto', true );
    Here we pass the tag name. Its slug will then be “posto-auto”. No need to sanitize “posto auto” because it’s hard coded and it’s impossible to inject malicious code into it.

Viewing 15 replies - 1 through 15 (of 22 total)

The topic ‘Automatically insert a tag, if…’ is closed to new replies.