Last active
May 9, 2017 07:39
-
-
Save alexanderhofstaetter/2e374266aac9c83feb06be667461d03c to your computer and use it in GitHub Desktop.
See: https://alexhofstaetter.at/query-custom-field-by-url-and-redirect-to-matching-post-in-wordpress/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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