且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

add_filter 和 add_action 之间的区别

更新时间:2023-12-04 11:01:04

pre_get_posts 是一个动作而不是一个过滤器.$query 是通过引用传递的,这就是代码 A 不返回任何内容的原因.

pre_get_posts is an action and not a filter. $query is passed by reference which is why CODE A works without returning anything.

CODE B 返回 $query 但该代码再次起作用,因为查询已通过引用传递.钩子的返回值没有赋值给任何东西.

CODE B returns $query but again that code works because query has been passed by reference. The return value of the hook isn't assigned to anything.

do_action_ref_array( 'pre_get_posts', array( &$this ) ); 

add_actionadd_filter 在不同的上下文中使用但代码相同(add_actionadd_filter 的别名)代码>).虽然发布的两组代码都可以使用,但正确的用法是 add_action.

add_action and add_filter are used in different contexts but the code is the same (add_action is an alias of add_filter). While both sets of code posted will work the correct usage is add_action.