Wordpress pre_get_posts and passing arguments

2019-09-07 17:31发布

问题:

I'm trying to get pre_get_posts to work with passing arguments. I have the following function in my function.php file of my theme.

function custom_pre_get_posts($query, $posttype, $poststatus, $paidvalue, $taxtype, $geo, $brands) {
  //my long query is here
}
add_action('pre_get_posts', 'custom_pre_get_posts', 10, 7);

I call this function via my taxonomy.php file via this code: do_action('pre_get_post','dealers', 'publish', '1', $taxtype, $geo, $brands);

On the output page, I am getting Missing argument 2 for custom_pre_get_posts(), Missing argument 3 for custom_pre_get_posts() etc.. all the way to argument 7.

What am I doing wrong?

回答1:

While it is very nice that you try to use action hooks, your hook name is wrong.
pre_get_posts is an existing hook name.
So you just have to rename your hook into something original.

A list off all hooks here