Custom Fields
Re-Envisioned
Support General By Post Field 'query args' with author

This topic contains 6 replies, has 2 voices, and was last updated by  cruz@assima.de 6 months ago.

  • Creator
    Topic
  • #13586
    Resolved
    cruz@assima.de
    Participant

    Hello

    I want to filter a Post Select with the actually author…

    array(
                'name'        => 'Firma auswahlen',
                'id'          => 'trainer_firma',
                'type'        => 'post',
    
            // Post type.
            'post_type'   => 'per_trainer_firmen',
    
            // Field type.
            'field_type'  => 'select_advanced',
    
            // Placeholder, inherited from <code>select_advanced</code> field.
            'placeholder' => 'Firma auswahlen',
    
                // Query arguments. 
                'query_args'  => array(
                    'post_status'    => 'publish',
                    'posts_per_page' => - 1,
                    'author'    => ???authorID???,
    
                ),
            ),

    ‘query args’ for author… If I try to write ‘author’ => 1, works fine… But I want to do this for any Authors…

    I think: get_current_user_id();
    But this is not working…

    can you help me?

    Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #13605

    Anh Tran
    Keymaster

    Do you mean query posts created by current user? Please clarify which user you want to set as author.

    #13621

    cruz@assima.de
    Participant

    Hi Anh

    “Do you mean query posts created by current user?”

    Yes!

    🙂

    #13635

    Anh Tran
    Keymaster

    I think the problem is calling get_current_user_id too early. This docs page has an interesting comment:

    Before the plugins_loaded action it returns 0.

    So you might want to make some change to:

    add_action( 'init', function() {
         add_filter( 'rwmb_meta_boxes', 'your_register_function' );
    } );
    #13759

    cruz@assima.de
    Participant

    Hello Anh

    Can you show me, what I have to do?
    I have not understand, what I have to change… 🙁

    Thank you Anh

    #13771

    Anh Tran
    Keymaster

    Assume you have this code to register your meta box:

    add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
    function prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array(
            // Your meta box code goes here
        );
    }

    Then change it to:

    add_action( 'init', function() {
         add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
    } );
    function prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array(
            // Your meta box code goes here
        );
    }
    #13816

    cruz@assima.de
    Participant

    Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.