# WordPress 插件 Elementor 查詢ID 自訂查詢範例 ###### tags: `WordPress` `Elementor` - ### 在元件模板的查詢ID中打上想要使用的名稱,並且來源選擇<font color="blue">[當前查詢]</font> ![](https://i.imgur.com/Q5lD01n.png) - ### 在WordPress主題文件夾的funtion.php中,新增add_action及add_filter ```php= <?php add_action( 'elementor/query/my_custom_filter', function( $query ) { global $wpdb; //此處將查詢的內容找出,並將其拿來做post_title的LIKE條件 $where = ' AND ' . $wpdb->posts . '.post_title LIKE \'%'.$query->get('s').'%\''; //只搜尋post文章 $query->set( 'post_type' , array('post')); //將查詢ID的內容設定為$where ※此處變數名稱與查詢ID無關,只是為了方便維護 $query->set( 'my_custom_filter' , $where ); }); add_filter( 'posts_where', 'my_custom_filter_posts_where', 10, 2 ); function my_custom_filter_posts_where( $where, &$wp_query ) { //上方的action會先觸發,所以在此處可以取得query中設定的my_custom_filter變數, //將其加入$where條件 if ( $my_custom_filter = $wp_query->get( 'my_custom_filter' ) ) { $where .= $my_custom_filter; } return $where; } ?> ``` - ### 參考 [Elementor Developers-Custom Query Filter](https://developers.elementor.com/custom-query-filter/)