I have a custom function that accepts two parameters $slug and $posttype, and checks if there exist a same post with that slug and post type.

My function:

function fnc_get_id_by_slug_and_posttype($slug, $post_type ) { 

  global $wpdb;

  $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID 
                                           FROM $wpdb->posts 
                                           WHERE post_name = %s 
                                           AND post_type= %s 
                                           AND post_status = 'publish'
                                          ", (string)$slug, $post_type ) ); 
  if ( $post ) 
    return get_post($post, $output)->ID; 
  return null; 
}

I already have two posts as below.

  • abc: post_title is abc and slug is abc
  • 123: post_title is 123 and slug is 123

My problem:

  • My custom function properly works and finds abc as duplicate when passed with abc
  • But it fails to detect duplicate when passed with 123 and returns null

I’m not mentioning post_type since they all belong to the same post type.

Is this something to do with data type?

Read more here: check slug duplicate not working for numeric slugs


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: