$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Filter posts by meta data using custom query|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Filter posts by meta data using custom query

matteradmin9PV0评论

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?

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far