I have problem with custom post type. In BO url’s are ok example: page.pl/post_type_name/post_title.
When I try use get_the_permalink() or the_permalink() it return url like:
/?post_type=cpt_wydarzenia&p=258
Code:
$args = array(
"label" => __( "Wydarzenia", "vilo" ),
'labels' => $labels,
'hierarchical' => false,
'supports' => array('title', 'editor'),
//'taxonomies' => array( 'category', 'post_tag' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => array(
'slug' => 'wydarzenia',
'with_front' => true
),
'capability_type' => 'post',
'menu_i
con' => 'dashicons-book',
'supports' => array(
'title',
'thumbnail',
'comments',
'editor'),
);
register_post_type( 'cpt_wydarzenia', $args );
flush_rewrite_rules();
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
get_permalink() (which is used by get_the_permalink() and the_permalink()) returns the “ugly” URL if the post is not published, e.g. draft or scheduled, but you can use the following function instead which temporarily sets the post status to publish so that we’d get the pretty URL.
function get_future_permalink( $id ) {
if ( $post = get_post( $id ) ) {
$post->post_status = 'publish';
return get_permalink( $post );
}
return '';
}
So instead of get_permalink( 258 ), you’d use get_future_permalink( 258 ), where 258 is the post ID.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0