Can't set/get custom public query_vars with add_rewrite_rule

I’ve added a url rewrite to my extras.php file and it’s working. But I can’t seem to set/get a custom public query variable. If I var_dump($qvars) below in lib/extras.php, it shows that ‘ptype’ has been pushed onto the end of the public variables array. But when I try to access the value in my template, it’s an empty string. Should I be putting this somewhere other than lib/extras.php or adding it to a different hook? Am I namespacing incorrectly? This worked fine in an non-sage, non-namespaced version on another site and is pretty much straight from the codex. Am I missing something obvious? Thanks.

In lib/extras.php:

namespace Sage\Rewrites;
function custom_query_vars( $qvars ) {
  $qvars[] = 'ptype';
  return $qvars;
add_filter( 'query_vars', __NAMESPACE__ . '\\custom_query_vars', 10, 1 );

function listen_rewrite_action() {
add_action( 'init', __NAMESPACE__ . '\\listen_rewrite_action' );

In template:

var_dump(get_query_var( 'ptype' ));  //NULL

Did you flush the rewrite rules by visiting the permalinks page?

Yes, I’ve tried that as well. Thanks for the quick response.

You need to use add_rewrite_tag instead of what you have now.