I have a custom post type called mls.
Most days, new posts are created in the custom post type.
I need a plugin that creates a REGULAR blog post with a list of all of the new posts in the custom post type of mls for each day. I'm really just learning php, but I've put together this code which I think is about 75% of the way there. I need someone to help me finish it up!
<?php
/*
Plugin Name: Post Secretary
Description: Publishes ( files away ) a blog post with a link to all new posts created within a specified custom post type.
Author: Scott Hack, and whomever decides to help me!
Author URI: http://www.scotthack.com
Version: .01
*/
//Do some sort of activation hook here. This syntax might be wrong.
register_activation_hook( __FILE__, 'xxx_myplugin_activate' );
function xxx_myplugin_activate() {
if (!wp_next_scheduled('xxx_my_task_hook')) {
wp_schedule_event( current_time ( 'timestamp' ), 'daily', 'xxx_my_task_hook' );
}
}
add_action( 'xxx_my_task_hook', 'xxx_my_task_function' );
//Need a deactivation hook of some sort to get rid of the scheduled event, right?
register_deactivation_hook( __FILE__, 'xxx_myplugin_deactivate' );
function xxx_myplugin_deactivate() {
wp_clear_scheduled_hook( 'xxx_my_task_hook');
}
function xxx_my_task_function() {
//Perform search for all posts in the custom post type published today.
$xxx_today = getdate();
$xxx_query = new WP_Query( 'post_type' => 'mls', 'year=' . $xxx_today["year"] . '&monthnum=' . $xxx_today["mon"] . '&day=' . $xxx_today["mday"] );
//Create the regular post with a list of those posts.
//Defaults for everything else including post type should be okay, but
//We set these. Post Title should be dynamic with a date field in it.
$xxx_my_post = array(
'post_title' => 'New listings for $xxx_today()',
'post_content' => 'This is my post content somehow...which I guess is now stored in $xxx_query above.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $xxx_my_post );
}
//That's all folks!
?>
Arnav Joy answers:
can you tell me what is the problem in this plugin you have created or what you want to achieve actually
Scott Hack comments:
I have not tested the code above, because I know it is incomplete.
I need the code to query the database and find all posts in the custom post type of mls. Then create a blog post each day with a list of those posts. The list is just a simple list of the post title and a link to the post. The rest of the code is just supporting to register the cron event to do the query and create the post.
So on Tuesday I have 5 new posts that are created in mls custom post type. So when the cron event is run, I want it to create a blog post with a list of those 5 mls posts. On Wed there are 3 mls posts. I want it to create a blog post showing a list of those 3 posts.
Does that make more sense?
Arnav Joy comments:
try this
<?php
/*
Plugin Name: Post Secretary
Description: Publishes ( files away ) a blog post with a link to all new posts created within a specified custom post type.
Author: Scott Hack, and whomever decides to help me!
Author URI: http://www.scotthack.com
Version: .01
*/
//Do some sort of activation hook here. This syntax might be wrong.
register_activation_hook( __FILE__, 'xxx_myplugin_activate' );
function xxx_myplugin_activate() {
if (!wp_next_scheduled('xxx_my_task_hook')) {
wp_schedule_event( current_time ( 'timestamp' ), 'daily', 'xxx_my_task_hook' );
}
}
add_action( 'xxx_my_task_hook', 'xxx_my_task_function' );
//Need a deactivation hook of some sort to get rid of the scheduled event, right?
register_deactivation_hook( __FILE__, 'xxx_myplugin_deactivate' );
function xxx_myplugin_deactivate() {
wp_clear_scheduled_hook( 'xxx_my_task_hook');
}
function xxx_my_task_function() {
//Perform search for all posts in the custom post type published today.
$xxx_today = getdate();
$xxx_query = new WP_Query( 'post_type' => 'mls', 'year=' . $xxx_today["year"] . '&monthnum=' . $xxx_today["mon"] . '&day=' . $xxx_today["mday"] );
if( $xxx_query->have_posts() ) :
$postBody = '<div>These are new custom posts of today.</div>';
$postBody .= '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post();
$postBody .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a><li>';
endwhile;
$postBody .= '</ul>';
else :
$postBody = '<div>no posts found today.</div>';
endif;
wp_reset_query();
//Create the regular post with a list of those posts.
//Defaults for everything else including post type should be okay, but
//We set these. Post Title should be dynamic with a date field in it.
$xxx_my_post = array(
'post_title' => 'New listings for $xxx_today()',
'post_content' => 'This is my post content somehow...which I guess is now stored in $xxx_query above.'.$postBody,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $xxx_my_post );
}
//That's all folks!
?>
Scott Hack comments:
This line is throwing an error upon activation. Not sure what is wrong with my syntax there.
$xxx_query = new WP_Query( 'post_type' => 'mls', 'year=' . $xxx_today["year"] . '&monthnum=' . $xxx_today["mon"] . '&day=' . $xxx_today["mday"] );
Parse error: syntax error, unexpected T_DOUBLE_ARROW
I can see that I've done something with mixing an array expression with a non array expression... but not enough to know how to fix it.
Arnav Joy comments:
use this one
$xxx_query = new WP_Query( 'post_type' => 'mls', 'year' => $xxx_today["year"] , 'monthnum' => $xxx_today["mon"] , 'day' => $xxx_today["mday"] );
Scott Hack comments:
Also, in reviewing this code...
if( $xxx_query->have_posts() ) :
What does the : do there? Should that be a { to open that if statement instead?
Scott Hack comments:
Still getting that error message.
Parse error: syntax error, unexpected T_DOUBLE_ARROW
Arnav Joy comments:
here is the full modified code
<?php
/*
Plugin Name: Post Secretary
Description: Publishes ( files away ) a blog post with a link to all new posts created within a specified custom post type.
Author: Scott Hack, and whomever decides to help me!
Author URI: http://www.scotthack.com
Version: .01
*/
register_activation_hook( __FILE__, 'xxx_myplugin_activate' );
function xxx_myplugin_activate() {
if (!wp_next_scheduled('xxx_my_task_hook')) {
wp_schedule_event( current_time ( 'timestamp' ), 'daily', 'xxx_my_task_hook' );
}
}
add_action( 'xxx_my_task_hook', 'xxx_my_task_function' );
//Need a deactivation hook of some sort to get rid of the scheduled event, right?
register_deactivation_hook( __FILE__, 'xxx_myplugin_deactivate' );
function xxx_myplugin_deactivate() {
wp_clear_scheduled_hook( 'xxx_my_task_hook');
}
function xxx_my_task_function() {
$xxx_today = getdate();
$xxx_query = new WP_Query( array('post_type' => 'mls', 'year' => $xxx_today["year"] , 'monthnum' => $xxx_today["mon"] , 'day' => $xxx_today["mday"] ));
if( $xxx_query->have_posts() ) :
$postBody = '<div>These are new custom posts of today.</div>';
$postBody .= '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post();
$postBody .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a><li>';
endwhile;
$postBody .= '</ul>';
else :
$postBody = '<div>no posts found today.</div>';
endif;
wp_reset_query();
$xxx_my_post = array(
'post_title' => 'New listings for $xxx_today()',
'post_content' => 'This is my post content somehow...which I guess is now stored in $xxx_query above.'.$postBody,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
wp_insert_post( $xxx_my_post );
}
?>
Arnav Joy comments:
also change new post title as follows:-
<?php
$xxx_my_post = array(
'post_title' => 'New listings for '.$xxx_today["mon"].'-'.$xxx_today["mday"].'-'.$xxx_today["year"] ,
'post_content' => 'This is my post content somehow...which I guess is now stored in $xxx_query above.'.$postBody,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
?>
Scott Hack comments:
I think I got it working. I had to change the line with the while statement on it. Not sure if it is a valid fix though.
Here is what I did. Basically, just added my $xxx_query instead of $the_query in this line... and then merged your two posts together with the post title with the date... and I am in business. Can you make sure I did this line right though so I don't have problems in the future. Will post full code in a moment.
<code>while ( $xxx_query->have_posts() ) : $xxx_query->the_post();<code>
Arnav Joy comments:
yes what you did was absolutely correct and you will not get any problem in present or in future.
Scott Hack comments:
<?php
/*
Plugin Name: Post Secretary
Description: Publishes ( files away ) a blog post with a link to all new posts created within a specified custom post type.
Author: Scott Hack, and whomever decides to help me!
Author URI: http://www.scotthack.com
Version: .01
*/
register_activation_hook( __FILE__, 'xxx_myplugin_activate' );
function xxx_myplugin_activate() {
if (!wp_next_scheduled('xxx_my_task_hook')) {
wp_schedule_event( current_time ( 'timestamp' ), 'daily', 'xxx_my_task_hook' );
}
}
add_action( 'xxx_my_task_hook', 'xxx_my_task_function' );
register_deactivation_hook( __FILE__, 'xxx_myplugin_deactivate' );
function xxx_myplugin_deactivate() {
wp_clear_scheduled_hook( 'xxx_my_task_hook');
}
function xxx_my_task_function() {
$xxx_today = getdate();
$xxx_query = new WP_Query( array('post_type' => 'mls', 'year' => $xxx_today["year"] , 'monthnum' => $xxx_today["mon"] , 'day' => $xxx_today["mday"] ));
if( $xxx_query->have_posts() ) :
$postBody = '<div>New Listings for Today.</div>';
$postBody .= '<ul>';
while ( $xxx_query->have_posts() ) : $xxx_query->the_post();
$postBody .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a><li>';
endwhile;
$postBody .= '</ul>';
else :
$postBody = '<div>No new posts found today.</div>';
endif;
wp_reset_query();
$xxx_my_post = array(
'post_title' => 'New listings for '.$xxx_today["mon"].'-'.$xxx_today["mday"].'-'.$xxx_today["year"] ,
'post_content' => $postBody,
'post_status' => 'publish',
//'post_author' => 1,
//'post_category' => array(8,39)
);
wp_insert_post( $xxx_my_post );
}
?>
Arnav Joy comments:
yes it will work