Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In continuation of this thread Mapping multiple URLs to same page in WordPress

is there any other way except redirect similar pattern URLs to same page. Our goal is to provide our guest and other authors there name/email specific URLs so that they can put those links on there website/blogs to encourange there readers to read there guest posts on our platform.

i can easily make sure that for each author who do not have a user profile in our system or don't want to create profile with us should have a unique username which we can associate with his/her posts

e.g if guest author have unique username is uniqu-author URL should be

www.mysite.com/author/uniqueauthor

now he can place this URL on his/her blog/website and when user came to our system we can redirect reader to a common author page and can extract uniqueauthor from URL,issue a query based on this username and come up with all his/her posts.

Any inputs will be much helpful for me to move forward

Thanks in advance

share|improve this question
    
errmm www.mysite.com/author/nicename is already built-in to wordpress. You don't have to do anything to make it work. see codex.wordpress.org/Function_Reference/get_author_posts_url. –  Ijaas Nov 22 '11 at 16:36
    
If you mean the user does not have a wordpress account and you want a link to display his/her posts then you can using a meta_value query, let me know if thats what you are looking for and i will hit you up. –  Ijaas Nov 22 '11 at 16:37
    
@Ijaas: yes that is what exactly i am looking for author will not have a account in our side so all we can store certain custom attribute in his/her post and we will make sure it will be unique for each author and we want to use them for our query, i looked at get_author_posts its a good function but sadly does not fit in to my needs –  Umesh Awasthi Nov 22 '11 at 16:44

1 Answer 1

up vote 0 down vote accepted

here you go:

add_action('init', 'wp_new_flush_rewrite_rules'); //Run this action only ONCE if not you are looking at 2 extra db queries on every page load so dont forget to comment it out
function wp_new_flush_rewrite_rules() {
    global $wp_rewrite; 
    $wp_rewrite->flush_rules();
}

add_action('generate_rewrite_rules', 'new_rewrite_rules');
function new_rewrite_rules( $wp_rewrite ) {

    $post_type = 'post';

    $new_rules = array(

        'writer/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?post_type='.$post_type.'&meta_key=writer&meta_value='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2),
        'writer/(.+?)/?$' => 'index.php?post_type='.$post_type.'&meta_key=writer&meta_value='.$wp_rewrite->preg_index(1),

    );

    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;   
    return $wp_rewrite;

}

add_filter('query_vars', 'add_query_vars');
function add_query_vars( $vars ){
    $vars[] = 'meta_key';
    $vars[] = 'meta_value';
    return $vars;
}

add_action('template_redirect', 'template_redirect_intercept');
function template_redirect_intercept(){

    global $wp_query;

    if ( $wp_query->get('writer') && file_exists( TEMPLATEPATH . '/author.php' ) ){
        $wp_query->is_404 = ''; $wp_query->is_archive = 1; $wp_query->is_post_type_archive = 1; // This is to prevent 404 errors on page 2,3,4 if posts for that page dont exist.
        include( TEMPLATEPATH . '/author.php' );
        exit;
    }

}

it is written to search for the 'writer' meta value of a post and display the author.php template.

the url will look like mysite.com/writer/WRITERNAME/ pagination also works so mysite.com/writer/WRITERNAME/page/2/ will work without an issue.

share|improve this answer
    
i tried your code but its not working.Sorry i am just trying my hands with php and wordpress.here is what i did. i added your code to the function.php file under theme directory,in one of the post i added following "custom field" fieldname="writer" fieldvalue="test-url" now i hit the following URL localhost/blog/wordpress/writer/test-url but its redirecing me to home page.Can you point what i am doing wrong as i have also author.php file in place –  Umesh Awasthi Nov 26 '11 at 6:34
    
when i looked at the log apache one i found following entries require_once() F:\\wamp\\www\\blog\\wordpress\\wp-config.php:90, referer: localhost/blog/wordpress/writer/test-url –  Umesh Awasthi Nov 26 '11 at 6:39
    
if you could send me an email at ijaas@dex-labs.com with some sort of IM contact details it would be a lot easier for me to help you out. Thanks. –  Ijaas Nov 27 '11 at 0:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.