how to make own slug
-
Hello, I found some code and its ful fill my needs.
foreach ( $database_results as $result ) { $name_details = array( 'post_title' => wp_strip_all_tags( $result->name_religion . ' ' . $result->name_gender . ' ' . $result->name_english . ' Name Meaning'), 'meta_input' => array( 'name_id' => $result->name_id, 'name_english' => $result->name_english, 'name_gender' => $result->name_gender, 'name_meaning' => $result->name_meaning, 'name_religion' => $result->name_religion, 'name_origin' => $result->name_origin, ), 'post_type' => 'baby_name', 'post_status' => 'publish', ); wp_insert_post( $name_details ); } }
so i see that this code create the title
'post_title' => wp_strip_all_tags( $result->name_religion . ' ' . $result->name_gender . ' ' . $result->name_english . ' Name Meaning'),
It is good and i want the url like this but i want to show title only name and url will be with religion, gender,name and extra content i have added.
Is this possible to do that ?
-
Hi @lahorimela
Yes, it is possible to have different values for the
post_title
and thepost_name
(which is used to generate the post URL). You can specify thepost_name
field when inserting the post like this:$name_details = array( 'post_title' => wp_strip_all_tags( $result->name_english . ' Name Meaning'), 'post_name' => wp_strip_all_tags( $result->name_religion . '-' . $result->name_gender . '-' . $result->name_english . '-name-meaning'), 'meta_input' => array( 'name_id' => $result->name_id, 'name_english' => $result->name_english, 'name_gender' => $result->name_gender, 'name_meaning' => $result->name_meaning, 'name_religion' => $result->name_religion, 'name_origin' => $result->name_origin, ), 'post_type' => 'baby_name', 'post_status' => 'publish', ); wp_insert_post( $name_details );
This will set the title of the post to “name Name Meaning” and the URL of the post to “
religion-gender-name-name-meaning
“. You can use thewp_unique_post_slug()
function to ensure that the generatedpost_name
value is unique.$post_name = wp_strip_all_tags( $result->name_religion . '-' . $result->name_gender . '-' . $result->name_english . '-name-meaning'); $post_name = wp_unique_post_slug( $post_name, 0, 'publish', 'baby_name', 0 ); $name_details = array( 'post_title' => wp_strip_all_tags( $result->name_english . ' Name Meaning'), 'post_name' => $post_name, 'meta_input' => array( 'name_id' => $result->name_id, 'name_english' => $result->name_english, 'name_gender' => $result->name_gender, 'name_meaning' => $result->name_meaning, 'name_religion' => $result->name_religion, 'name_origin' => $result->name_origin, ), 'post_type' => 'baby_name', 'post_status' => 'publish', ); wp_insert_post( $name_details );
This will generate a unique
post_name
value based on your provided input, but with a suffix added to ensure uniqueness.thankyou faisal
I have a question for you @faisalahammad this is the code that creates posts in custom post type and gets data from a custom table from the database.
add_action( 'admin_init', 'my_admin' ); function my_admin() { add_meta_box( 'baby_name_meta_box', 'Name Details', 'display_baby_name_meta_box', 'baby_name', 'normal', 'high' ); } function display_baby_name_meta_box() { ?> <table width="100%"> <tr> <td>Name</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_english', true ); ?>" readonly /></td> </tr> <tr> <td>Gender</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_gender', true ); ?>" readonly /></td> </tr> <tr> <td>Meaning</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_meaning', true ); ?>" readonly /></td> </tr> <tr> <td>Religion</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_religion', true ); ?>" readonly /></td> </tr> <tr> <td>Origin</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_origin', true ); ?>" readonly /></td> </tr> <tr> <td>Short Name</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_short', true ); ?>" readonly /></td> </tr> <tr> <td>Lucky Number</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_number', true ); ?>" readonly /></td> </tr> <tr> <td>Zodiac</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_zodiac', true ); ?>" readonly /></td> </tr> <tr> <td>Rank</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_rank', true ); ?>" readonly /></td> </tr> <tr> <td>Color</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_color', true ); ?>" readonly /></td> </tr> <tr> <td>Day</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_day', true ); ?>" readonly /></td> </tr> <tr> <td>Metal</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_metal', true ); ?>" readonly /></td> </tr> <tr> <td>Stone</td> <td><input type="text" size="40" name="names" value="<?php echo get_post_meta( get_the_ID(), 'name_stone', true ); ?>" readonly /></td> </tr> </table> <?php } add_action( 'wp', 'lahorimela_insert_into_baby_name_cpt' ); function lahorimela_check_for_similar_meta_ids() { $id_arrays_in_cpt = array(); $args = array( 'post_type' => 'baby_name', 'posts_per_page' => -1, ); $loop = new WP_Query($args); while( $loop->have_posts() ) { $loop->the_post(); $id_arrays_in_cpt[] = get_post_meta( get_the_ID(), 'id', true ); } return $id_arrays_in_cpt; } function lahorimela_query_names_table( $name_available_in_cpt_array ) { global $wpdb; $table_name = $wpdb->prefix . 'names'; if ( NULL === $name_available_in_cpt_array || 0 === $name_available_in_cpt_array || '0' === $name_available_in_cpt_array || empty( $name_available_in_cpt_array ) ) { $results = $wpdb->get_results("SELECT * FROM $table_name"); return $results; } else { $ids = implode( ",", $name_available_in_cpt_array ); $sql = "SELECT * FROM $table_name WHERE id NOT IN ( $ids )"; $results = $wpdb->get_results( $sql ); return $results; } } function lahorimela_insert_into_baby_name_cpt() { $name_available_in_cpt_array = lahorimela_check_for_similar_meta_ids(); $database_results = lahorimela_query_names_table( $name_available_in_cpt_array ); if ( NULL === $database_results || 0 === $database_results || '0' === $database_results || empty( $database_results ) ) { return; } foreach ( $database_results as $result ) { $name_details = array( 'post_title' => wp_strip_all_tags( $result->name_english), 'post_name' => wp_strip_all_tags( $result->name_religion . '-' . $result->name_gender . '-' . $result->name_english . '-name-meaning'), 'meta_input' => array( 'name_id' => $result->name_id, 'name_english' => $result->name_english, 'name_gender' => $result->name_gender, 'name_meaning' => $result->name_meaning, 'name_religion' => $result->name_religion, 'name_origin' => $result->name_origin, 'name_short' => $result->name_short, 'name_number' => $result->name_number, 'name_zodiac' => $result->name_zodiac, 'name_rank' => $result->name_rank, 'name_color' => $result->name_color, 'name_day' => $result->name_day, 'name_metal' => $result->name_metal, 'name_stone' => $result->name_stone, ), 'post_type' => 'baby_name', 'post_status' => 'publish', ); wp_insert_post( $name_details ); } }
But I am not happy with this thing, I do lots of tests to make section for names and am tired of that. I have 80000 names to display. I have done with custom taxonomy as well but every time my site doesn’t work properly got the error (HTTP ERROR 449) after the refresh, it works, some times CSS doesn’t work when I start working with names.
Do you have any better solution that how i can make a names section with 80000 names with multiple religions?
Sorry @lahorimela, it is impossible to provide a coding solution in this manner. It is difficult to assess the code without testing it and providing my feedback.
@faisalahammad ok let say i have an excel sheet with 80000 rows of data, what is the best way to add this data to database and show in wordpress.
We will create pages to display data.
Christian Boy Names
Christian Girl Names
Muslim Boy Names
Muslim Girl Names
Hindu Boy Names
Hindu Girl NamesAnd 1 template for all names
There are a few different ways you could add the data from your Excel sheet to the WordPress database and display it on your website. Here are a few options:
1. Use a plugin like WP All Import to import the data from your Excel sheet into WordPress. This plugin can automatically create posts or custom post types for each row of data in your Excel sheet. You can then use a custom template to display the data on your website.
2. Write a custom script to import the data from your Excel sheet into the WordPress database. You can use the
wp_insert_post()
function to create posts or custom post types for each row of data and theadd_post_meta()
function to add metadata for each post. You can then use a custom template to display the data on your website.3. Use the WordPress REST API to import the data from your Excel sheet into the WordPress database. You can use a tool like Postman to send POST requests to the WordPress REST API to create posts or custom post types for each row of data. Using a custom template, you can then use the REST API to retrieve the data and display it on your website.
Which option you choose will depend on your specific needs and your level of familiarity with WordPress development. Option 1 is the easiest and quickest option, but it may not be as flexible as the other options. Options 2 and 3 will require more development work but will give you more control over the import process and the imported data.
- The topic ‘how to make own slug’ is closed to new replies.