Skip to content

Instantly share code, notes, and snippets.

@alexanderhofstaetter
Last active May 9, 2017 07:39
Show Gist options
  • Select an option

  • Save alexanderhofstaetter/2e374266aac9c83feb06be667461d03c to your computer and use it in GitHub Desktop.

Select an option

Save alexanderhofstaetter/2e374266aac9c83feb06be667461d03c to your computer and use it in GitHub Desktop.
// 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' )
]);
if($posts) {
wp_redirect( get_permalink( $posts[0]->ID ) );
exit();
}
else {
global $wp_query;
$wp_query->set_404();
status_header(404);
}
}
}
add_action( 'template_redirect', 'cfqar_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment