Wordpress WP_查询传递一个唯一标识符,以便稍后用于操作挂钩?

Wordpress WP_查询传递一个唯一标识符,以便稍后用于操作挂钩?,wordpress,Wordpress,我在WP codex中找不到它,但是有没有办法为自定义WP_查询传递唯一标识符 我想识别一个特定的查询,以便对其执行操作挂钩函数 $args = array( 'my_custom_id' => 'customidentifier' <-- something like this?? 'category_name' => 'news', 'posts_per_page' => 3 ); $my_query = new WP_Query( $

我在WP codex中找不到它,但是有没有办法为自定义WP_查询传递唯一标识符

我想识别一个特定的查询,以便对其执行操作挂钩函数

$args = array(
    'my_custom_id' => 'customidentifier'   <-- something like this??
    'category_name' => 'news',
    'posts_per_page' => 3
);
 
$my_query = new WP_Query( $args );
提前谢谢

试试这个

$args = array(
    'my_custom_id' => 'customidentifier',
    'category_name' => 'news',
    'posts_per_page' => 3
);
$my_query = new WP_Query( $args );
然后,要钩住动作,请添加以下内容

function checkQueryForMy_custom_id( $my_query ){
  if( $my_query->get( 'my_custom_id' ) === 'customidentifier' ){
     //continue
  }
}
add_action( 'pre_get_posts', 'checkQueryForMy_custom_id', 10 );

我没有测试这个,但应该可以工作。

如果需要唯一标识符,则需要使用wp\u create\u nonce函数


是的,您可以,它将被添加到query_vars属性数组中,您可以稍后在操作中检查它

add_action(
    'pre_get_posts',
    function( $wp_query_obj ) {
        if ( ! empty( $wp_query_obj->query_vars['my_custom_id'] ) ) {
            // ...
        }
    },
    100,
    1
);

谢谢这并不是我想要的。我需要能够使用pre_get_posts hook,并有条件地仅更改该类型的查询。@SeanRasmussen我已根据您的需要编辑了我的响应。