0

I'm trying to filter post by two things.

  1. By the current logged in user.
  2. By meta data (disc_id) from the post.

Note: The value for "disc_id" is a copy of the post ID from the custom post type it is published from. The post would be created on the front end using gravity forms. So lets assume the form is attached to a page with a post ID of 7020. When the post is published, the meta value for "disc_id" gets the value 7020. What i'd like to do is filter all the post with that particular value.

I've managed to get the first part working, which shows the post of the current user, but not the second part, post based on meta data. Please see the code below and can anyone kindly let me know a possible work around for this? I'll greatly appreciate. Thank you!

 function fl_builder_loop_query_args_filter( $query_args ) {

    if ( 'dis_board' == $query_args['settings']->id ) {

    $query_args['post_type'] = array( 'discussion_board');

    $query_args['author'] = get_current_user_id();

    }

    return $query_args;

    }

    add_filter( 'fl_builder_loop_query_args', 'fl_builder_loop_query_args_filter' );

1 Answer 1

1

I assume that the fl_builder_loop_query_args hook is from Beaver Builder, which means that the $query_args are args passed to the WP_Query. source

In that case you can push some custom field parameters to the $query_args to get all the posts with matching meta value.

I think it should work something like this,

function fl_builder_loop_query_args_filter( $query_args ) {
    if ( 'dis_board' == $query_args['settings']->id ) {
        $query_args['post_type'] = array( 'discussion_board');
        $query_args['author'] = get_current_user_id();

        $query_args['meta_key'] = 'disc_id'; // the assigned meta key
        $query_args['meta_value_num'] = 7020; // value we're looking for
    }
    return $query_args;
}
add_filter( 'fl_builder_loop_query_args', 'fl_builder_loop_query_args_filter' );

Could test this one and see if it works the way you want?

3
  • Hi Anitti Koskinen, You're right. The hook is for the beaver builder. However, I added the two lines of the code you supplied it only returns all the post by the current logged in user and not those with the meta value of "7020". Oct 20, 2018 at 11:00
  • Hi, it could be that the meta value is saved as string and not as a number. You could then try using $query_args['meta_value'] = "7020";. A comparison argument might also help $query_args['meta_compare'] = "LIKE"; Oct 20, 2018 at 11:19
  • Ahhh, Nice. The "meta_value" args did the trick. I'm then able to compare the value with the post type ID it was created from '$query_args['meta_value'] = $post_id;' . Thanks for sharing Sir! Oct 20, 2018 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.