• Hi,

    I’m inserting posts into wordpress using a script which is loaded through CRON. Before inserting each post I am checking whether the post with the same title exists or not. Below is the snippet I’m using:


    if( is_null(get_page_by_title( $page_title, 'OBJECT', $post_type )) ) {
    // Create post object
    $my_post = array(
    'post_title' => "Food & Wine", // this is example and it actually inserts $page_title.
    'post_content' => 'Dummy content',
    'post_status' => 'draft',
    'post_author' => 1,
    'post_type' => $post_type,
    );

    // Insert the post into the database
    wp_insert_post( $my_post );
    } else {
    echo 'Post already exists.';
    }

    Now this insertion part works pretty well, but it stores ‘&’ as html entity (‘&’) in the database. So in the database, the post title looks like “Food & Wine”. In the list of posts admin screen, the title looks fine with ‘&’ and once I go to the edit screen, it shows ‘&’.

    This causes the first ‘if’ condition to return TRUE and it inserts a duplicate post. My database collation type is “utf8_general_ci”. Well this works on my local MAMP installation, but fails on my shared host.

    I would like to know what is wrong in the code and how should this situation be handled. Also is it suggested to escape html entities for the ‘post_content’?

    Thanks.

  • The topic ‘HTML Entities in Post Title’ is closed to new replies.