Hello in the "action init" to fairly large function, but I would like to add a filter to modify the vars variables, for example the wordpress post ID. That is to say:

add_action( 'init',function(){

    add_filter( 'query_vars',function($vars){
        $vars[] = array('ID' => $myid);
         return $vars;


This is possible?

EDIT: I am doing A/B/C tests of pages and with the same url I want to show a page with another ID, ie, edit the ID of the current post to display the complete content of another post.

  • This is a perfect example of an XY problem- you are asking about what you think the solution is rather than stating what the problem is that you are trying to solve. Yes, you can add a filter inside init, but this approach to your problem is completely wrong. Your comment below "I am doing A/B/C tests of pages and with the same url I want to show a page or another ID" should be your question. – Milo May 7 '17 at 16:25
  • @Milo You're absolutely right, but I've tried and I have not been able to edit the post ID. Right now I'm using a very bad option that is using file_get_content – jcarlosweb May 8 '17 at 17:57

To alter the page ID before the query is run, hook the request filter.

If you're using pretty permalinks, pagename will be set, you can overwrite pagename with another page slug:

function wpd_265903_request( $request ) {
    if( isset( $request['pagename'] ) ){ // any page
        $request['pagename'] = 'some-other-slug';
    return $request;
add_filter('request', 'wpd_265903_request');

or you can unset pagename and set page_id:

function wpd_265903_request( $request ) {
    if( isset( $request['pagename'] ) ){
        unset( $request['pagename'] );
        $request['page_id'] = 106;
    return $request;
add_filter( 'request', 'wpd_265903_request' );
  • thanks thanks, work perfectly, Sorry I can not give you points. – jcarlosweb May 9 '17 at 14:29

Yes, it's possible to do something like that. And in fact, if you want to remove actions/filters, then you will be almost certainly be required to use one hook to remove it.

For instance, plugins load before the theme. So if a theme adds a hook to the init hook from the functions.php file like so:

add_action( 'init', 'wpse_265903_init' );

Plugins would not be able to remove that action until after the theme gets setup:

add_action( 'after_setup_theme', function() {
  remove_action( 'init', 'wpse_265903_init' );
} );

Similarly, if you want some hooks to run only after the admin_init hook, then you can do something like this:

add_action( 'admin_init', function() {
  add_action( 'the_post', function() {
  } );
} );

The init hook fires on every request though, so I'm not sure the purpose of adding hooks from that particular hook.

  • I understand perfectly, and apologize if you do not explain to me well, I am doing a A/B/C tests of pages and with the same url I want to show a page with another ID, and now I am testing several filters and action and I can not do it. – jcarlosweb May 8 '17 at 17:59
  • I answered the question you asked. Not the one that I was magically supposed to know that you wanted answered. – Nathan Johnson May 8 '17 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.