1

I am fetching some testimonials data from db and based on the slugs, I am getting the links to use further. Here is my code

function createPageForTestimonials($slug, $title, $content, $companyName){
    $new_page = array(
        'slug' => $slug,
        'title' => $title,
        'content' => $content
    );

    //check if page exists
    if (the_slug_exists($slug)) {
        $url = get_permalink( get_page_by_path( $slug ) );
        return get_permalink( get_page_by_path( $slug ) );
    }
    else {
        $new_page_id = wp_insert_post(array(
            'post_title' => $new_page['title'],
            'post_type' => 'page',
            'post_name' => $new_page['slug'],
            'comment_status' => 'closed',
            'ping_status' => 'closed',
            'post_content' => $new_page['content'],
            'post_status' => 'publish',
            'post_author' => 1,
            'menu_order' => 0
        ));

        return get_permalink($new_page_id);
    }
}

//check if page exists by its slug
function the_slug_exists($post_name) {
    global $wpdb;
    $post_name = sanitize_title($post_name);
    if($wpdb->get_row($wpdb->prepare("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A'))) {
        return true;
    } else {
        return false;
    }
}

the function is returning home URL when I move the code to server here, but working fine and returning expected urls on localhost. Here is the live website. I have also debug the issue by checking that if (the_slug_exists($slug)) returns true every time but the url returned is of homepage, which should not happen. Please let me know how I can debug this issue better or if I am doing something wrong here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.