So I want to add the following ?matchPostId=$matchId
in post_name
on wordpress
using wp_insert_post();
I tried using urlencode(); and urldecode();
but still can't get it to work, checked everywhere but seems like the question mark and equals sign is always slashed out in the url of my post. Does anyone know why it does that? Here is my code:
$questionm = '?';
$equalsm = '=';
$tipsPage = $questionm . 'matchPostId' . $equalsm . $matchId;
$post_idpost = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'post_name' => $tipsPage,
'post_title' => $value['test']
'post_status' => 'publish',
'post_type' => 'post',
'post_content' => "test"
)
);
Also tried this:
$questionm = ('?');
$equalsm = ('=');
urlencode($questionm);
urlencode($equalsm);
$post_idpost = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'post_name' => urldecode($questionm) . 'matchPostId' . urldecode($equalsm) . $matchId,
Same problem, the question mark and equals sign is removed from the url.
WP sanitizes postname in the wp_insert_post function.
if ( empty($post_name) ) {
if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
$post_name = sanitize_title($post_title);
} else {
$post_name = '';
}
} else {
// On updates, we need to check to see if it's using the old, fixed sanitization context.
$check_name = sanitize_title( $post_name, '', 'old-save' );
if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
$post_name = $check_name;
} else { // new post, or slug has changed.
$post_name = sanitize_title($post_name);
}
}
Source: http://hookr.io/functions/wp_insert_post/ circa line 3094