Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My script is generating a duplicate post even after I explicitly state that the post_type should be a page. It also ignores the $post_title, which I do not want to create a duplicate post if the title already exists. Can anyone figure out why it's creating a post instead of a page?

function av_generate_page($post_id) {

$post_info = get_post($post_id);
$post_slug = $post_info->post_name;
$post_title = $post_info->post_title;
$page_exists = get_page_by_title($post_title);
if($page_exists){ exit(); }
$detail_level = get_post_meta($post_id, 'detail_level', true);
$formats = wp_get_post_terms($post_id, 'formats', array('fields'=>'slugs'));

$page = array(
    'post_type' => 'page',
    'post_title' => $post_title,
    'post_content' => '[['.$post_slug.']]',
    'post_status' => 'publish',
    'post_parent' => 8800
);
if( !$page_exists ) {
    if (in_array('brief-segment',$formats)){
        // Do not create a page for brief segments
    }else{
        $new_page_id = wp_insert_post($page, false);
        update_post_meta($new_page_id,'suf_hide_page_title','on');
        update_post_meta($new_page_id,'_wp_page_template','no-sidebars-no-comments.php');
            if(in_array('internal-only',$formats)){
              add_post_meta($new_page_id,'InternalOnly','yes',true);
            }
    }
}else{
    // Do not create a page
}   
} add_action('publish_post', av_generate_page, 12);

EDIT How would I pass the current post id to be used as my ID for generating a page?

As a premise for why I'm embedding a post into a page (provided that that page has not already been created), I'm creating a hierarchical treeview of pages. Because posts are flat, I need to create a page each time I create a post and move it to the appropriate place in the hierarchy. Why am I not simply creating a page instead? Because I need to create multiple pages for each post based on the multiple categories that they apply to. Yes, they are tagged with the categories in the post, but I still need to provide the list of pages that I cannot do with a post. Therefore, I'm creating a page that can live in a hierarchy and references a post that I can thus reference in other pages as well.

share|improve this question
    
What are you actually trying to do? Where does the $post_id parameter come from? If you know it to be a valid post id, then you know it exists, no? Or do you just want to prevent to pages from having the same title? –  nus Jul 16 '12 at 22:14
    
I'm referencing posts in multiple pages so that they can be found in various hierarchies. The Codex suggests that the post_id is passed with the add_action function: codex.wordpress.org/Function_Reference/add_action –  AlxVallejo Jul 17 '12 at 1:26
add comment

1 Answer

I still don't exactly get it. I would advise you to take it one step at a time first. Your function takes in a post_id, so that is an existing post. If you then get that post's title and then you call get_page_by_title, obviously that is going return you the same than get_post already did. Your logic is flawed I think. Could you just write a list of steps in english of what you would like to achieve?

If what you try is just stop a post/page from being published if one with the same title already exist, just know that in itself it already exists, because wp makes autosaves and drafts and the like. In essence you probably want to prevent the post_status going from 'draft' to 'published'.

If that's what you are after, check this question: Prevent post from being published if custom fields not filled

To figure out if a post with the same title already exists, the codex entry for get_page_by_title explains how:

Retrieves a post given its title. If more that one post uses the same title, the post with the smallest ID will be returned.

So:

if( $post_id !== get_page_by_title( $post_title ) )

    $page_exits = true;

If you call exit(), you terminate execution. It is probably not what you want and will break your website. If you want to stop the function, just use return; Once you return, nothing else will be executed from this function call, so it doesn't make sense to do another test for $page_exists later, because if it existed that test wouldn't even be executed because the function had already returned...

Maybe some sort of learning php book or some tutorials could help you advance...

Wordpress is not necessarily the easiest thing to program for...

share|improve this answer
    
Thanks for the return tip. get_page_by_title searches through pages, not posts by default. So publishing a post and checking if a page with that title exists is perfectly fine in my view. I'll amend the question to provide a premise. I only added the additional if(!$page_exists) as a backdrop to the first conditional. –  AlxVallejo Jul 17 '12 at 16:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.