Making same query by different user yields different results in Multisite

I am trying to build WordPress in multisite environment.

In my blog, I have a custom post type called company, and users can modify company information in front-end.

In order to avoid duplicates, each company has a fixed slug. That is, even when the company name (post title) is changed from ‘My Company’ to ‘Your Company’, its slug (post name) remains as ‘my-company’. And I run a validation to detect slug duplicates as below when users edit the company name.

The logic I validate the company slug duplicate is that:

  • (1) Get the inputted company name
  • (2) Call AJAX ‘check_slug_duplicate()’ with the inputted name, post ID of current post, and post type (company).
  • (3) AJAX checks if a company with the inputted name (slug) already exists, and if it exists, the inputted name is not available
  • (4) However, if the existing company in (3) is nothing but a company currently being edited, the inputted name is still available


So, right now, I have a company named ‘test2’ with slug ‘test2’.

When log-in as a super-admin (the network admin) and edit the company information other than its name, the duplicate checking routine returns ‘true’, meaning that $isAvailable is true.

However, in the same page, if I do the same thing while logged-in as an administrator of that specific blog, the duplicate checking routine returns ‘false’, meaning that $isAvailable is false, no matter what name I input.

I manually checked the values of all variables, and the variables all properly have intended values. So I am suspecting that there is something to look into about ‘users’, but I do not know where to start look into.

Please advise.

Company Edit Form

<?php $post_id = $post->ID; ?>

<form id="company_form" class="form-horizontal">

    <!-- Post ID -->
        <input type="hidden" id="_post_id" value="<?php echo $post_id; ?>" />

    <!-- Name -->    
    <div class="form-group">
        <label class="col-lg-3 control-label">Name</label>
        <div class="col-lg-9">
            <?php $name = get_the_title( $post_id ); ?>
            <input <?php echo $disabled; ?> type="text" id="_name" name="_name" value="<?php echo $name; ?>" class="form-control" />

    <div class="form-group">
        <label class="col-lg-3 control-label">Name</label>
        <div class="col-lg-9">
            <input type="submit" name="_go" class="btn" value="Make Changes" />


Copmany Name Validation (Duplicate Check) Code

jQuery(document).ready(function() {

            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            row: {
                valid: 'field-success',
                invalid: 'field-error'
            fields: {
                _name: {
                    validators: {
                        notEmpty: {
                            message: 'The company name is required'
                        remote: {
                            message: 'The company name already exists',
                            url: ajaxurl,
                            type: 'post',
                            delay: 500,
                            data: {
                                '_name': function() {
                                    return jQuery( "#_name" ).val();
                                '_post_id' : function() {
                                    return jQuery( "#_post_id" ).val();
                                '_post_type': 'company',
                                'action': 'check_slug_duplicate'
           }) // end formValidation
           .on('success.form.fv', function(e) {
            // Now the form validation is successfully done

            // Prevent from submission

            // Do something here



AJAX: check_slug_duplicate

add_action( 'wp_ajax_nopriv_check_slug_duplicate', 'check_slug_duplicate' );
add_action( 'wp_ajax_check_slug_duplicate', 'check_slug_duplicate' );

function check_slug_duplicate() {

    global $wpdb;

    $_name = $_REQUEST['_name'];
    $_post_type = $_REQUEST['_post_type'];
    $_post_id = $_REQUEST['_post_id'];

    if( !$_name || !$_post_type || !$_post_id ) {

    // Sanitize name (post_title) to slug (post_name) before querying
    $_slug = sanitize_title( $_name );

    $exists = fnc_get_id_by_slug_and_posttype( $_slug, $_post_type );

    if( $exists && ( (int)$_post_id != (int)$exists ) ) {
        $isAvailable = false;
    } else {
        $isAvailable = true;

    echo json_encode( array(
        'valid' => $isAvailable,



FUNCTION: fnc_get_id_by_slug_and_posttype()

function fnc_get_id_by_slug_and_posttype( $_slug, $_post_type ) {

    global $wpdb;

    if( !$_slug || !$_post_type ) {
        return -1;

    $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID 
                                           FROM $wpdb->posts 
                                           WHERE post_name = %s 
                                           AND post_type= %s 
                                           AND post_status = 'publish'
                                          ", $_slug, $_post_type ) );

    if ( $post ) {
        return get_post($post, $output)->ID;
    } else {
        return null;


Read more here: Making same query by different user yields different results in Multisite

Leave a Reply

Your email address will not be published. Required fields are marked *