• Resolved egycode

    (@egycode)


    I’m happy to share my solution to add category and other taxonomy sub categories pages with other users who want to track visits to their categories pages, this will also work for any other taxonomy type categories other than default wordpress taxonomy category type!

    All you need to add 5 lines of code to the plugin and the rest is added to the currently used WordPress functions.php file!

    Add this code in this file in the plugin directory “/plugins/koko-analytics/src/class-script-loader.php” below line no 72

    The following code will log visits for category pages by using the WordPress function is_category() so you can modify the function by using similar function like is_product_category() and use the term_key “koko_product_post_id” where “product” is the specific mark for product taxonomy pages – this is known for every custom WordPress plugin that use it’s own taxonomy and so on!

    		if ( is_category() ) {
    			$cat_pot_id = get_term_meta( get_queried_object_id(), 'koko_category_post_id', true);
    			if ( $cat_pot_id != false ) {
    				return $cat_pot_id;
    			}
    		}

    And add this to WordPress theme functions.php file

    //Create post for current taxonomy page
    function ec_create_taxonomy_post_data() {
    	global $wpdb;
    	$queried_object = get_queried_object();
    	//Check if we are in category or any taxonomy page like products category page
    	//This will work with other custom wordpress plugins that create other taxonomies other than
    	//default wordpress categories!
    	if ( isset( $queried_object->taxonomy ) && $queried_object->taxonomy != '' && !empty( $queried_object->taxonomy ) && isset( $queried_object->term_id ) ) {
    		//Check if the post exists for our current taxonomy page
    		$tax_exist = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_name = %s LIMIT 1", 'koko-' . $queried_object->taxonomy . '-slug-' . $queried_object->term_id ), ARRAY_A );
    		//Update taxonomy post data in case category or taxonomy name or title has been changed!
    		if ( $tax_exist != false && !empty( $tax_exist ) && $tax_exist['post_title'] != $queried_object->name ) {
    			$postdata = array(
    				'post_title'     => $queried_object->name,
    			);
    			//for debugging
    			//echo '<pre>' . print_r($tax_exist, true) . '</pre>';
    			$where = array( 'ID' => $tax_exist['ID'] );
    			if ( false === $wpdb->update( $wpdb->posts, $postdata, $where ) ) {
    				error_log( 'Could not update post for taxonomy in the database' );
    				return;
    			}		
    		}
    		//Insert post data for current taxonomy page
    		if ( $tax_exist == false ) {
    			$postdata = array(
    				'post_title'     => $queried_object->name,
    				'post_status'    => 'publish',
    				'post_name'      => 'koko-' . $queried_object->taxonomy . '-slug-' . $queried_object->term_id,
    				'comment_status' => 'closed',
    				'ping_status'    => 'closed',
    				'guid'           => $queried_object->term_id,
    				'post_type'      => 'koko_' . $queried_object->taxonomy . '_page',
    			);
    			if ( false === $wpdb->insert( $wpdb->posts, $postdata ) ) {
    				error_log( 'Could not insert post for taxonomy in the database' );
    				return;
    			}
    			$post_id = (int) $wpdb->insert_id;
    			$koko_tax_post_id = $post_id;
    			update_term_meta( $queried_object->term_id, 'koko_' . $queried_object->taxonomy . '_post_id', $koko_tax_post_id );
    		}
    	}
    }
    //Add action if plugin is loaded!
    if ( defined( 'KOKO_ANALYTICS_VERSION' ) ) {
    	add_action( 'template_redirect', 'ec_create_taxonomy_post_data', 11 );
    }
    
    function alter_koko_tax_page( $permalink, $post, $leavename ) {
    	if ( preg_match( '~ koko_(.*)_page ~siU', ' ' . $post->post_type . ' ', $tax_type ) == true ) {
    		$post_tax_id = (int) $post->guid;
    		//Get the permalink for taxonomy page
    		$permalink = get_term_link( $post_tax_id, $tax_type[1] );
    		return $permalink;
    	}
    	return $permalink;
    }
    //Add filter if plugin is loaded!
    if ( defined( 'KOKO_ANALYTICS_VERSION' ) ) {
    	add_filter( 'post_link', 'alter_koko_tax_page', 99, 3 );
    }

    I hope this help as a temp solution for users interested in this option where it’s important to track taxonomy and category pages.

    • This topic was modified 1 year, 6 months ago by egycode.
    • This topic was modified 1 year, 6 months ago by egycode.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Working solution to count taxonomies and categories pages visits’ is closed to new replies.