0

My idea is collect post from subsites to main site in Network WP MS and open it there. I have found nice class https://github.com/miguelpeixe/WP_Query_Multisite for Multisite Querying. Now i want to open posts in single page with structure: mysite.com/blog/%site_id%/%postname%. I tryed to create query varable, add_rewrite_tag and supposted actions. Also I tryed to include this tag in WP Admin Option => Peamalink as structure element for a custom structure. But after query mysite.com?site_id=2&p=5 i had result like: mysite.com/blog/2/post-name?site_id=2. Query variable stayed in URL line. If i delete ?site_id=2 link countinue to work. I think I do logic wrong due of low level of link rewriting and hope to get ideas and adviced from more expirienced people. There my class without "working code rubbish":

class WP_Rewrite_Multisite_Link {

    function __construct() {

        add_filter('query_vars', array($this, 'query_vars'));
        add_action('pre_get_posts', array($this, 'pre_get_posts'), 100);
        add_filter( 'available_permalink_structure_tags', array($this, 'available_permalink_structure_tags')); 
        add_action('init', array($this, 'register_rewrite_rules'), 10);
        add_filter( 'post_link', array($this, 'post_link'), 10, 3);
    }


    function query_vars($vars) {

        $vars[] = 'site_id';

        return $vars;
    }

    function pre_get_posts($query) {
        //Setup table for getting posts
        if( ($query->get('site_id')) && ( ($query->get('p')) || ($query->get('name')) ) ) {

            global $wpdb;

            $exist_sites = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");

            if ( in_array($query->get('site_id'), $exist_sites)) {
                $wpdb->posts = $wpdb->get_blog_prefix($query->get('site_id')) . 'posts';
                $this->posts_site_id = (int) $query->get('site_id');
            }
        }

    }

    function register_rewrite_rules() { 

        global $wp_rewrite;

        $wp_rewrite->add_rewrite_tag( '%site_id%', '([0-9]+)', 'site_id=' );

        //There i tryed to use different rewrite_rules, not understainding how do it

        $wp_rewrite->add_permastruct('site_id', '%site_id%');


        $wp_rewrite->flush_rules();

    }

    function available_permalink_structure_tags ( $tags ) {
        global $blog_id;

        $tags['site_id'] = $blog_id;

        return $tags;
    }

    function post_link($permalink, $post, $leavename) {
        //Replace tag in permalink. It isn't best way, i think
        global $blog_id;

        if ( strpos( $permalink, '%site_id%') ) {
            if( isset($this->posts_site_id) ){
                $permalink = str_replace('%site_id%', $this->posts_site_id, $permalink);
            }
            else {
                $permalink = str_replace('%site_id%', $blog_id, $permalink);
            }       
        }
        return $permalink;


    }   
    }
}
new WP_Rewrite_Multisite_Link();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.