Combining multiple user taxonomy of users in a single URL

I created a user user taxonomy for the user and its work for a single taxonomy.

In the Bellow reference guide, I created a user user taxonomy.

Reference Guide: http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress

Following is the structure of the taxonomy

(registered taxonomy) profession Dietitian (under the profession ) (registered taxonomy) city NewYork(under the city) 

my slug expert name

Here I want to display the result of the filter, which the Dietitian user in NewYork city.so will display all the users who have selected the option above in the profile.

Now I want a URL like www.test.com/expert/dietitian/newyork with the user who selected Dietitian, NewYork in the user profile. And is there a decision to merge Dietitian,NewYork

A single-term URL works like: - www.test.com/expert/dietitian/

+5
source share
2 answers

I assume that you spent a lot of time on the justintadlock tutorial, so I will participate only with its parts, which are necessary for multiple filtering and displaying taxonomy on the interface.

Before you read the long text, you can try http://playground.georgemanousarides.com/

So first, first. The logical basis is the same for ONE TAXONOMY AND MULTIPLE TAXONOMIES. After registering the taxonomies and their conditions and completed all the necessary work of wp-admin, we will need:

A) A page (allows you to call it a TAX PAGE) in which visitors can select a taxonomy request.

B) Another page (let's call it TAX RESULTS PAGE), in which the results of the TAX-QUERY PAGE page will be displayed.

Let the immersion in


SINGLE TAXONOMY

TAX PAGE

After completing the tutorial, the page on which the taxonomy links are displayed should look in its primary form as follows:

single taxonomy tax request page

Visitors β€œSelect” which taxonomy they want to see by simply clicking on the links.

Note:

A) Links for:

  • The rebate will look like this: www.example.com/user/profession/developer/
  • The city will look like this: www.example.com/user/city/gotham/

B) The user / city and user / profession plums are defined in the function: my_register_user_taxonomy β†’ register_taxonomy β†’ rewrite β†’ slug

TAX RESULTS PAGE

By clicking on the links mentioned above, you will be taken to a page (which should be defined by you, of course), which displays all users in the same taxonomy period.

This is achieved by creating custom taxonomy templates.

You can create a taxonomy-profession-developer .php file for processing a single taxonomy and term or taxonomy-profession .php for processing a single taxonomy and all its "terms" or .php taxonomy for all taxonomies and their terms.

In my opinion, if you do not want your server to be flooded with template files that you manually created, you should use the .php taxonomy and create a common tablet for all taxonomies that do not allow you to get the results that you want to automatically display.

Note:

A) To repeat through users and select only those that are under the desired taxonomy, a special request is required (in the taxonomy.php file), as indicated and explained in the tutorial.

B) The required parameters must be in the Post Name parameter from the wp-admin β†’ β†’ permalinks parameters so that wordpress can find your taxonomy, select the taxonomy.php file and provide useful information about the selected taxonomy that can be used in the taxonomy.php file.


MULTIPLE TAXONOMIES

TAX PAGE

We need to create a page where visitors will select terms from custom taxonomies. This page will provide a page of TAX RESULTS (taxonomy.php) with the necessary conditions via a link (how to get variables **) to make a request.

** In order to have fairly permanent links on the TAX-RESULTS page, we need to add the following rewrite function (found here ) to the .php function and update the permalinks in wp-admin:

 function eg_add_rewrite_rules() { global $wp_rewrite; $new_rules = array( 'user/(profession|city)/(.+?)/(profession|city)/(.+?)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4), 'user/(profession|city)/(.+)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) ); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' ); 

note that

If you register more taxonomies, the "profession | city" function should become "profession | city | tax1 | tax2 | tax3".

Thus, the TAX-QUERY page will:

HTML

 <div id="taxonomies"> <h1>Find your professional</h1> <form> <?php if ( $taxonomies = get_object_taxonomies( 'user' ) ) : //get all taxonomies under the object_type "user" ( The second parameter given to the function my_register_user_taxonomy of the tutorial ) foreach ( $taxonomies as $taxonomy ) : ?> <p> <ul> <fieldset id="<?php echo esc_attr( $taxonomy ); ?>"> <!-- group the form by taxonomies --> <legend><h2>Choose <?php echo $taxonomy; ?></h2></legend> <?php if ( $terms = get_terms( $taxonomy ) ) : //get taxonomy terms foreach ( $terms as $term ) : ?> <li><input type="checkbox" value="<?php echo esc_attr( $term -> slug ); ?>"> <?php echo $term -> name; ?></li> <?php endforeach; endif; ?> </fieldset> </ul> </p> <?php endforeach; endif; ?> </form> <a id="multiTaxSubmit" href="<?php echo esc_attr( get_home_url() . '/user' ); ?>">SUBMIT</a> <!-- this link is processed by jQuery to provide the taxonomy.php with the proper values. The href attribute is the base url needed by the taxonomy.php --> </div> 

JQUERY (can be placed in an external file)

 $( document ).ready( function() { $( '#multiTaxSubmit' ).click( function ( event ){ event.preventDefault(); //prevent default link url from loading var taxQuerySubmit = $( this ), hasChecked = 0; querySlug = taxQuerySubmit.attr( 'href' ); //get multitax url base from link href attr $( '#taxonomies fieldset' ).each( function() { //iterate each taxonomy var checkedTerms = $( this ).find( 'input:checked' ), checkedLength = checkedTerms.length; //how many terms has the user selected if ( checkedLength ) { hasChecked += checkedLength; querySlug += '/' + $( this ).attr( 'id' ) + '/'; //add taxonomy slug to query url checkedTerms.each( function( index, value ) { var comma = ( index == checkedLength-1 ? '' : ',' ); querySlug += $( this ).val() + comma; } ); } } ); if ( hasChecked ) { window.location = querySlug; } else { alert( 'Please enter some criteria.' ); } } ); } ); 

TAX RESULTS PAGE (taxonomy.php)

 <?php $USERS_BY_TAX = array(); if ( $taxonomies = get_object_taxonomies( 'user' ) ) { //get all taxonomies under the object_type "user" ( The second parameter given to the function my_register_user_taxonomy of the tutorial ) foreach ( $taxonomies as $tax_key => $taxonomy ) { eval( '$check = $' . $taxonomy . ';' ); // Check if the taxonomy exists in the url. eval outputs $check = $profession, $check = $city etc. if ( !$check ){ unset( $taxonomies[ $tax_key ] ); continue; } eval( '$term_names = explode( ",", $' . $taxonomy . ' );' ); // get terms array from $$taxonomy which gives $profession,$city, the values of which are given through the url as such: $profession="designer,developer" $USERS_BY_TAX[ $taxonomy ] = array(); foreach ( $term_names as $term_name ) { $term_obj = get_term_by( 'name', $term_name, $taxonomy ); //get term object for each given term $users_in_term = get_objects_in_term( $term_obj -> term_id, $taxonomy ); // find users with term if ( !empty( $users_in_term ) ) { $USERS_BY_TAX[ $taxonomy ] = $USERS_BY_TAX[ $taxonomy ] + array_fill_keys( $users_in_term, $term_name ) ; } } } } /* $USERS_BY_TAX array has all the users for each taxonomy but we only need those who exist in all taxonomies */ if ( $taxonomies ) { $RESULTS = $USERS_BY_TAX; // keep the initiate array intact $matched = array_pop( $USERS_BY_TAX ); // first array to compare $TAXS = $taxonomies; array_pop( $taxonomies ); if ( !empty( $USERS_BY_TAX ) ) { foreach ( $taxonomies as $taxonomy ) { if ( !empty( $USERS_BY_TAX ) ) $matched = array_intersect_key( $matched, $USERS_BY_TAX[ $taxonomy ] ); } } } ?> /* DISPLAY */ <?php if ( $matched ) : foreach ( array_keys( $matched ) as $user_id ): ?> <div class="user-entry"> <?php echo get_avatar( get_the_author_meta( 'email', $user_id ), '96' ); ?> <h2><?php the_author_meta( 'display_name', $user_id ); ?></h2> <?php if ( in_array( 'profession', $TAXS ) ) : ?><h3>Profession: <?php echo $RESULTS[ 'profession' ][ $user_id ]; ?></h3><?php endif;?> <?php if ( in_array( 'city', $TAXS ) ) : ?><h3>City: <?php echo $RESULTS[ 'city' ][ $user_id ]; ?></h3><?php endif;?> <?php echo wpautop( get_the_author_meta( 'description', $user_id ) ); ?> </div> <?php endforeach; else: ?> <div class="user-entry"> <h2>We are sorry. No results match your criteria.</h2> <h3>Please <a href="javascript:history.back()">go back</a> and search again!</h3> </div> <?php endif; ?> 
+1
source

This rewrite rule should work (provided that the β€œprofession” and β€œcity” are registered taxa):

Code:

 function custom_rewrite_rules() { add_rewrite_rule('^profession/(.*)/city/(.*)?', 'index.php?profession=$matches[1]&city=$matches[2]', 'top'); } add_action('init', 'custom_rewrite_rules'); 

Remember to reset the rewrite rules after saving this code on your site.

URL: http://yourdomain.com/profession/dietitian/city/newyork/

0
source

Source: https://habr.com/ru/post/1238613/


All Articles