dimanche 26 juin 2016

Auto-Generate Posts Using Data from Other Posts in Wordpress

I have a theme that works pretty great for what I need. I just need a little more functionality to it. I would say I'm a bit newer to wordpress as a programmer, but do have some previous php experience. I need to somehow auto-generate a post from a project post when said project post reaches completion. Generally when it is done it will have a Bar that reaches 100, turns green and says successful. When this happens and the end date has passed I need for that to kick off a new post with certain data already built into it (user IDs who contributed only have access, etc.) so that the content that has been created has somewhere to go to be viewed. If I can't do something like this it can spell a horrific amount of excess manual work in the future for me. I attempted to insert it into the div and when that broke the page I then attempted to do it in the functions.php thinking that I may be missing some order of operations that is specific to wordpress. Here's the code I am attempting to use:

<?php

function newvid_create_post() {

$post_id = -1;

// Setup the author, slug, and title for the post
$author_id = 1;
$slug = 'postingtest';
$title = 'MySite Library Test';

// If the page doesn't already exist, then create it
if( null == get_page_by_title( $title ) ) {

// Set the post ID
$post_id = wp_insert_post(
array(
'comment_status'    =>  'closed',
'ping_status'   =>  'closed',
'post_author'   =>  $author_id,
'post_name' =>  $post_id,
'post_title'    => $title,
'post_status'   =>  'publish',
'post_type' => 'post'
)
);

// Otherwise, it'll stop
} else {

// Using -2 to indicate that the page with the title already exists
$post_id = -2;

} // end if

} // newvid_create_post
add_filter( 'after_setup_theme', 'newvid_create_post' );
The various calls to this function would look like this:

$post_id = newvid_create_post()
if( -1 == $post_id || -2 == $post_id ) {

}

?>

</div>

I also attempted something like this, but was unsure of how I would integrate the DB variables into the post automatically.

function show_post_type(){

    $labels = array(
        'name' => 'Show',
        'singular_name' => 'Show',
        'add_new' => 'Add Show',
        'all_items' => 'All Shows',
        'add_new_item' => 'Add Show',
        'edit_item' => 'Edit Show',
        'new_item' => 'New Show',
        'view_item' => 'View Show',
        'search_item' => 'Search Show',
        'not_found' => 'No Shows Found',
        'not_found_in_trash' => 'No Shows Found in Trash',
        'parent_item_colon' => 'Parent Reward',     
    );
    $args = array(
        'labels' => '$labels',
        'public' => true,
        'has_archive' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'support' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
            'revisions',
        ),
        'taxonomies' => array('category', 'post_tag',),
        'menu_position' => 6,
        'exclude_from_search' => false,
    );
    register_post_type('show', $args);

}

And for validation on those pages something like:

<?php $user_ID = get_current_user_id();

$query = "SELECT * FROM wp_posts WHERE '$user_ID'='$post_author' AND post_type='project' OR post_type='funder';

$result=mysqli->query($query);

$count=mysqli->_num_rows($result);

if(count==1){

echo "Access Granted." // I want to put access to the actual media here

}
else{

echo "No Access" // I am looking to put the redirect to the payment page for access here which // will insert them into the table as a "funder".

}
?>

The table I use is setup for custom post types seen in the query above. I have to make sure that they match the post_parent or their post siblings seen above so that the page/post can be viewed only by the people who made or contributed to the project and assigning new user roles with specific permissions for only one post while allowing for an insert into each post when someone else joins each time would be really painful. For some reason I think I may be putting things in the wrong area/format but can't be sure and I have been trying for over a month and a half now. If anyone has any thoughts at all or can point me in the right direction I would be really grateful.

Aucun commentaire:

Enregistrer un commentaire