Creating a custom field with value from other custom fields

Support General Creating a custom field with value from other custom fieldsResolved

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #31792
    goninski@gmail.com[email protected]
    Participant

    Hi

    Unfortunately the custom field variables in rankmath work only for textfields (text, textarea, wysiwyg). That's why I'm not able to create the seo-snippet content dynamicly from my other fields, like taxonomy etc.

    So I'm looking for a solution to create a new text field, which fetches the values from other custom fields. Is there any possibility for this? (note: the custom html field does not work either in rankmath)

    Thanks in Advance for your help
    Regards
    François

    #31796
    Long NguyenLong Nguyen
    Moderator

    Hi,

    It is possible. You can use Meta Box's action hooks like rwmb_{$meta_box_id}_after_save_post to update a field value from another custom field after saving the post.

    Please get more details on the documentation
    https://docs.metabox.io/actions/#rwmb_after_save_post
    https://docs.metabox.io/rwmb-set-meta/

    #31808
    goninski@gmail.com[email protected]
    Participant

    great, I'll try. Thanks a lot !

    #32260
    goninski@gmail.com[email protected]
    Participant

    Hello

    I was basicly succesfull with your help. One thing is not working though:

    When using this code on frontend of the post, it is working fine and showing the first name of the artist (fetched from custom taxonomy field):


    <?php
    $terms = get_the_terms( get_queried_object_id(), 'artists' );

    foreach ( $terms as $term ) {
    $term_name = rwmb_meta( 'fg_meta_artist_name', ['object_type' => 'term'], $term->term_id );
    $term_name_first = rwmb_meta( 'fg_meta_artist_name_first', ['object_type' => 'term'], $term->term_id );

    $value $term_name_first;
    echo $value;
    }

    ?>

    When trying to fill an empty custom field (text) with the same value, the field it not filled up:


    <?php
    add_action( 'rwmb_after_save_post', function( $post_id ) {

    < code from above (without echo) >

    update_post_meta( $post_id, 'fg_taxo_object_artist_names', $value );
    } );

    ?>

    Both fields are text fields.
    Do you see any reason why this is not working?

    Thanks a lot in advance.
    Regards, François

    #32279
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Did you add the frontend submission shortcode on the single post? Following the code, I think you are trying to update the field value fg_taxo_object_artist_names of the submitted post by the term meta of the current post which shows the frontend submission form.

    #32340
    goninski@gmail.com[email protected]
    Participant

    Hello

    Yes the 1st code is implemented on the single post template (cpt) (php code block in oxygen builder).

    The 2nd Code is in a code snippet and should update the field 'fg_taxo_object_artist_names' after saving a post in the editor (no frontend submission).

    But the field is not updatet. I guess the field values of the taxonomies are not fetched?
    (why is it working on frontend but not on backend?)

    Hope I was able to explain more clear.

    #32347
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Yes, it might not work on the backend because the function get_queried_object_id() is used to get the post ID on the frontend. You can follow this topic to get the post ID on the backend.
    https://support.metabox.io/topic/get-the-post-id-as-value/

    #32426
    goninski@gmail.com[email protected]
    Participant

    Hi,

    Thanks for your hint. Unfortunately this to high for me. I can't see where to change to code to get the term id on backend.

    This is my code. I don't know if you can provide the correct snippet.
    If not, thanks anyway. I understand.

    <?php
    add_action( 'rwmb_after_save_post', function( $post_id ) {
    
    $terms = get_the_terms( get_queried_object_id(), 'artists' );
    foreach ( $terms as $term ) {
        $term_name = rwmb_meta( 'fg_meta_artist_name', ['object_type' => 'term'], $term->term_id );
        $term_name_first = rwmb_meta( 'fg_meta_artist_name_first', ['object_type' => 'term'], $term->term_id );
    
        $value = $term_name_first;
    }
            update_post_meta( $post_id, 'fg_taxo_object_artist_names', $value );
    } );
    ?>
    #32448
    Long NguyenLong Nguyen
    Moderator

    Hi,

    In the backend, you can try to use the variable $post_id to pass the post ID to the function get_the_terms()

    add_action( 'rwmb_after_save_post', function( $post_id ) {
    $terms = get_the_terms( $post_id, 'artists' );
    ...
    } );
    #32455
    goninski@gmail.com[email protected]
    Participant

    Hi and thank you.

    Unfortunately the code is not working. I see I need to leverage my php knowledge urgently to get things like this work.

    Hava a nice day.

    #41866
    Joachim AusterJoachim Auster
    Participant

    It would be so nice, if I could use my metabox metafields as variables in Rankmath Snippet Editor WITHOUT any programming, just using the variable like %mb_page_metadata_kurzbeschreibung_auszug%

    Any idea, when this would be possible?

    I think a LOT of users would be happy about that.

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.