0

I have a wordpress function that adds a query string 'nocfcache=1' to a single page url.

function nocfcache_query_string( $url, $id ) {
    if( 42 == $id ) {
        $url = add_query_arg( 'nocfcache', 1, $url );
    }
    return $url;
}

add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );

Issue: How to use multiple page ids in the function so as to make sure they will all have the query string appended to the url.

What I have tried so far:

function nocfcache_query_string( $url, $id ) {
    $id = array (399, 523, 400, 634, 636, 638);
    if(in_array($post->ID, $id)) {
        $url = add_query_arg( 'nocfcache', true, get_permalink( $post->ID ));
        return $url;
        exit;
    }
}

add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );
2
  • That should work - try taking out the space in array (399... so it's just array(399... Commented May 15, 2019 at 19:46
  • @WebElaine taking out the space in array (399... didn't work, I flushed the permalinks but when I go to the edit page there is no permalink nor view page available, clicking on view (from Pages) gets me back to the wp-admin/pages. Commented May 15, 2019 at 20:01

1 Answer 1

0

In your second code you use undefined $post variable. Probably you meant a global $post, but as the second parameter to the filter is passed the ID of the post for which the link is created, and this does not necessarily have to be the current post of the loop.

Try this:

add_filter( 'page_link', 'nocfcache_query_string', 10, 2 );

function nocfcache_query_string( $url, $id ) 
{
    $ids = array (399, 523, 400, 634, 636, 638);
    if( in_array($id, $ids) ) 
    {
        $url = add_query_arg( 'nocfcache', 1, $url );
    }
    return $url;
}
1
  • yes I thought of it as a global $post, it worked like a charm, you saved me hours of frustation!! Commented May 15, 2019 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.