Filter posts by meta data using custom query

admin2025-06-07  58

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' );

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' );
Share Improve this question edited Oct 18, 2018 at 0:30 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Oct 17, 2018 at 23:48 Kendell DanielKendell Daniel 131 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749248083a317596.html

最新回复(0)