I am making my custom permalinks and want to get the post author id in it I have tried many things but all in vain, Please help me on this one.
STEP 1 add this into your functions.php
Guidance here
https://wordpress.stackexchange.com/questions/112719/how-to-use-author-id-in-post-permalink
function wpse_112719_pre_post_link( $permalink, $post, $leavename ) {
if ( strpos( $permalink, '%author%' ) !== false ) {
$authordata = get_userdata( $post->post_author );
$author_id = $authordata->ID;
$permalink = str_replace( '%author%', $author_id, $permalink );
}
return $permalink;
}
add_filter( 'pre_post_link', 'wpse_112719_pre_post_link', 10, 3 );
function wpse_112730_add_rewrite_rules() {
add_rewrite_rule(
"([0-9]+)/(.*)",
'index.php?name=$matches[2]',
'top'
);
}
add_action( 'init', 'wpse_112730_add_rewrite_rules' );
STEP 2 login to admin end and set the permalinks Custom Structure some thing like this : /%postname%/%author%
Related
I have a woocommerce website hosted on domain https://www.micropediaglobal.com.
I have one requirement that my different branch managers located at different locations should see the orders as per the locators and process their orders only.
Can someone please guide here ?
The Website Development Company website does not seems to be on wordpress, however you can bifurcate or process woocommerce orders location wise as below:
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['xerox_shop_name'] ) ) {
update_post_meta( $order_id, 'xeroxshopmeta', sanitize_text_field( $_POST['xerox_shop_name'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Xerox Center Name').':</strong> ' . get_post_meta( $order->id, 'xeroxshopmeta', true ) . '</p>';
}
//add custom field column
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns[ 'order_actions' ] );
//edit this for your column(s)
//all of your columns will be added before the actions column
$new_columns['xeroxColumn'] = 'Xerox Center Name';
//stop editing
$new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
return $new_columns;
}
add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns )
{
$custom = array(
'xeroxColumn' => 'xeroxshopmeta',
);
return wp_parse_args( $custom, $columns );
}
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
global $post;
$data = get_post_meta( $post->ID );
//start editing, I was saving my fields for the orders as custom post meta
//if you did the same, follow this code
if ( $column == 'xeroxColumn' ) {
echo get_post_meta( $post->ID, 'xeroxshopmeta', true );
}
}
I am looking for a solution in order to remove /%category%/ from my permalinks for only one category ("general").
My permalink is currently set to /%category%/%postname%/. How can I create the following URLs only for the "general" category /%postname%/ ?
Thanks!
You can set custom structure in Permalinks Settings: Dashboard - Settings>Permalinks
This works for me:
function remove_uncategorized( $permalink, $post, $leavename ) {
if( $post->post_type != 'post' ) return $permalink;
$cats = get_the_category($post->ID);
if( ! count($cats) ) return $permalink;
usort($cats, '_usort_terms_by_ID');
$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
$category_object = get_term( $category_object, 'category' );
return _clear_uncategorized($category_object, $permalink);
}
function _clear_uncategorized($cat, $permalink) {
if( $cat->slug == 'sin-categoria' ) {
return str_replace('%category%/', '', $permalink);
}
$parent = $cat->parent;
if ( !$parent )
return $permalink;
return _clear_uncategorized($parent, $permalink);
}
add_filter( 'pre_post_link', 'remove_uncategorized', 9, 3 );
At this time I have 2 custom post-type item, projects, made custom field for attach project post in item post (custom field save post_id)
I want use attached project post-name in link:
Current link structure:
domain / {item-post-type-slug} / {item-name}
I want change to:
domain / {project->post_name} / {item-name}
With current code should flush rewrite rules on every save/delete action on projects post-type?
How to redirect old link structure to the new ?
add_action( 'init', 'add_rewrite_rules' );
function add_rewrite_rules() {
$arg = array(
'post_type' => 'projects',
'no_conflict' => '1',
'posts_per_page' => '-1'
);
$projects= new WP_Query($arg);
while($projects->have_posts() ) : $projects->the_post();
global $post;
add_rewrite_rule( $post->post_name.'/([^/]+)/?$', 'index.php?item=$matches[1]', 'top');
endwhile;
}
add_filter( 'post_type_link', 'custom_permalinks', 10, 2 );
function custom_permalinks( $permalink, $post ) {
if ( $post->post_type !== 'item' )
return $permalink;
$project_id = get_field('project', $post->ID);
$project = get_post($project_id);
$new_permalink = str_replace("item", $project->post_name, $permalink);
return $new_permalink;
}
For flush rewrite rules using
function flush_project_links( $post_id) {
if ( get_post_type( $post_id ) != 'projects' )
return;
flush_rewrite_rules();
}
add_action('delete_post', 'flush_project_links', 99, 2);
add_action('save_post', 'flush_project_links', 99, 2);
but it's work only on second time when update post
UPDATED:
flush rewrite fixed:
function flush_project_links( $post_id) {
if ( get_post_type( $post_id ) != 'projects' )
return;
add_rewrite_rules()
flush_rewrite_rules();
}
add_action('delete_post', 'flush_project_links', 99, 2);
add_action('save_post', 'flush_project_links', 99, 2);
and removed add_action( 'init', 'add_rewrite_rules' );
I'm looking for a way to put a Woocommerce product ID in a URL, e.g.:
domain.com/product-category/id/product-title
The ID is the product's post ID, e.g. "12092". Automatically created by WooCommerce in Wordpress.
Can this be done in a way? Either easily through the Wordpress permalinks settings or in a different way through hacking my way into the files.
This nearly works:
<?php
add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'product' ){
return home_url( 'p/' . $post->ID );
} else {
return $link;
}
}
add_action( 'init', 'wpse33551_rewrites_init' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
'product/([0-9]+)?$',
'index.php?post_type=product&p=$matches[1]',
'top' );
}
?>
But the output is:
domain.com/p/45
I'd like it to be:
domain.com/p/45/title-of-the-post
Thanks!
Why so complicated? Go to settings -> permalinks and click "Shop based URL" and save. Now you can see that the permalink structure in the "Custom structure". It should looks like this now:
/shop/%product_cat%/
Now add the post_id to the permalink setting like this:
/shop/%product_cat%/%post_id%/
Save it and go ahead.
You can use $post->post_name to get the slug.
Use a more permissive regular expression to accept any url that ends with /p/[id][anything]
<?php
add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);
function wpse33551_post_type_link( $link, $post = 0 ){
if ( $post->post_type != 'product' )
return $link;
return home_url( 'p/' . $post->ID . '/' . $post->post_name );
}
add_action( 'init', 'wpse33551_rewrites_init' );
function wpse33551_rewrites_init(){
add_rewrite_rule(
'p/([0-9]+)?.*?$',
'index.php?post_type=product&p=$matches[1]',
'top'
);
}
?>
You might need to 'reset' your permalinks in the settings menu for WordPress to accept the new rule.
I want to use something like:
http://example.com/%category_id%/%postname%/
for the permalink structure.
For example, if a post has a category with an ID of 3, then the URL for the post will be
http://example.com/3/post-name/
Does anyone know how this can be done? I don't mind modifying WordPress core.
This code adds the %category_id% rewrite tag, and filters post permalinks to replace them with the actual category ID (lowest if there are multiple categories). You can place this in a plugin or in your theme file.
add_action( 'init', 'so6159452_init' );
function so6159452_init()
{
add_rewrite_tag( '%category_id%', '([0-9]+)' );
}
add_filter( 'post_link', 'so6159452_post_link', 10, 2 );
function so6159452_post_link( $permalink, $post )
{
if ( false !== strpos( $permalink, '%category_id%' ) ) {
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' ); // order by ID
$category_id = $cats[0]->cat_ID;
} else {
// Error: no category assigned to this post
// Just use a dummy variable
$category_id = '0';
}
$permalink = str_replace( '%category_id%', $category_id, $permalink );
}
return $permalink;
}