Skip to content

Instantly share code, notes, and snippets.

View alexanderhofstaetter's full-sized avatar

Alexander Hofstätter alexanderhofstaetter

View GitHub Profile
// Redirect to the query URL if the URL matches the regex
function cfqar_register_rewrite_rule()
{
add_rewrite_rule('^(([0-9]{1,2})([a-z]|[A-Z])([0-9]))/?$', 'index.php?qr=$matches[1]', 'top');
}
add_action( 'init', 'cfqar_register_rewrite_rule' );
// Try to get the post with the queried custom field value and do the actual redirect (or throw an 404)
function cfqar_redirect()
{
if( get_query_var( 'qr' ) )
{
$posts = get_posts([
'post_type' => 'product',
'meta_key' => 'qr-code',
'meta_value' => get_query_var( 'qr' )
]);
// Add custom query variable
function cfqar_register_query_var( $vars )
{
$vars[] = 'qr';
return $vars;
}
add_filter( 'query_vars', 'cfqar_register_query_var' );