I am trying to create a cron job that turns WooCommerce products from publish to draft, and to draft from publish depending on stock and the low stock amount field. At first i get it to run, i had to tweak it a bit and now nothing happens. I adjusted some code i found here: How to auto draft WooCommerce product on specific date?
Here is the code i have been using:
// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'check_daily_for_change_product_status' ) ) {
wp_schedule_event( time(), 'daily', 'check_daily_for_change_product_status' );
}
// Hook into that action that'll fire every three minutes
add_action( 'check_daily_for_change_product_status', 'check_daily_for_change_product_status_func' );
function check_daily_for_change_product_status_func() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => array("publish", "draft")
);
$get_products = new WP_Query( $args );
if( $get_products->have_posts() ){ while ( $get_products->have_posts() ) { $get_products->the_post();
$produkt_kat = get_the_terms( get_the_ID(), 'product_cat' );
foreach ($produkt_kat as $kat_id)
{
$lagerstyring = false;
$slaa_til = get_field( "lavt_lager", $kat_id );
if ($slaa_til != false)
{
$lagerstyring = true;
$to = get_field("deaktiverings_emails", $kat_id);
break;
}
}
$product_id = get_the_ID();
$admin_lager = $product_id->get_manage_stock();
$low_stock_amount = $product_id->get_low_stock_amount();
$lager = $product_id->get_stock_quantity();
$produktnavn = $product_id->get_name();
$produkturl = get_permalink($product_id);
$tidspunkt = current_time();
if ($admin_lager == true && $lagerstyring == true)
{
if ($low_stock_amount >= $lager)
{
$my_post = array(
'ID' => get_the_ID(),
'post_status' => 'draft',
);
wp_update_post( $my_post );
wp_mail( $to, "Vare slået fra", "Denne vare er blevet automatisk slået fra da den har en lav lagerbeholdning: <br /><b>" . $produktnavn . "</b><br />" . $produkturl . "<br />" . $tidspunkt, 'Content-type: text/html', );
}
if ($lager > $low_stock_amount && $low_stock_amount != null)
{
$my_post = array(
'ID' => get_the_ID(),
'post_status' => 'publish',
);
wp_update_post( $my_post );
wp_mail( $to, "Vare slået til", "Denne vare er blevet automatisk udgivet, da dens lagerbeholdning er øget: <br /><b>" . $produktnavn . "</b><br />" . $produkturl . "<br />" . $tidspunkt , 'Content-type: text/html', );
}
}
} wp_reset_postdata(); }
}
To anyone who might need this code, I figured it out and it works now.
// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'check_daily_for_change_product_status' ) ) {
wp_schedule_event( time(), 'daily', 'check_daily_for_change_product_status' );
}
// Hook into that action that'll fire every three minutes
add_action( 'check_daily_for_change_product_status', 'check_daily_for_change_product_status_func' );
function check_daily_for_change_product_status_func() {
$args = array(
'post_type' => "product",
'posts_per_page' => -1,
'post_status' => array("publish", "draft")
);
$get_products = new WP_Query( $args );
if( $get_products->have_posts() ){ while ( $get_products->have_posts() ) { $get_products->the_post();
$produkt_kat = get_the_terms( get_the_ID(), 'product_cat' );
foreach ($produkt_kat as $kat_id)
{
$lagerstyring = false;
$slaa_til = get_field( "lavt_lager", $kat_id );
if ($slaa_til != false)
{
$lagerstyring = true;
$to = get_field("deaktiverings_emails", $kat_id);
break;
}
}
global $product;
$product_id = get_the_ID();
$admin_lager = $product->get_manage_stock();
$low_stock_amount = $product->get_low_stock_amount();
$lager = $product->get_stock_quantity();
$produktnavn = $product->get_name();
$produkturl = $product->get_permalink();
$tidspunkt = date('d/m/Y H:i:s', current_time('timestamp'));
if ($admin_lager == true && $lagerstyring == true)
{
if ($low_stock_amount >= $lager)
{
$my_post = array(
'ID' => get_the_ID(),
'post_status' => 'draft',
);
wp_update_post( $my_post );
wp_mail( $to, "Vare slået fra", "Denne vare er blevet automatisk slået fra da den har en lav lagerbeholdning: <br /><b>" . $produktnavn . "</b><br />" . $produkturl . "<br />" . $tidspunkt, 'Content-type: text/html', );
}
if ($lager > $low_stock_amount && $low_stock_amount != null)
{
$my_post = array(
'ID' => get_the_ID(),
'post_status' => 'publish',
);
wp_update_post( $my_post );
wp_mail( $to, "Vare slået til", "Denne vare er blevet automatisk udgivet, da dens lagerbeholdning er øget: <br /><b>" . $produktnavn . "</b><br />" . $produkturl . "<br />" . $tidspunkt , 'Content-type: text/html', );
}
}
}
wp_reset_postdata(); }
}
Related
I have this code to display term metadata but for some reason, it's not showing up / Data is blank Sorry for not making it clear the first time
UPDATED
if ( 'new_column2' == $column_name ) {
/*Begin*/
foreach((get_term_meta( $post->ID, 'crosssell' )) as $category) {
$cat_name = $category->name . '</br>';
$cat_id = $category->term_id . '</br>';
$crosssell = get_term_meta( $cat_id, 'crosssell', true );
}
/*End*/
$value = $crosssell;
}
return $value;
}
what am i doing wrong
You're assigning variables but not actually outputting anything.
foreach( ( get_term_meta( $post->ID, 'crosssell' ) ) as $category ) {
echo $category->name . '</br>';
echo $category->term_id . '</br>';
echo get_term_meta( $category->term_id, 'crosssell', true );
}
This fixed the problem I had.
if ( 'new_column2' == $column_name ) {
/*Begin*/
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
//$woo_categories = get_categories( $prod_cat_args );
$woo_categories = get_the_terms( $post->ID, 'product_cat' );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
}
$cat_term_id = $woo_cat_id;
$crosssell = get_term_meta( $cat_term_id, 'crosssell', true );
/*End*/
$value = $crosssell;
}
return $value;
}
I am working on a plugin to send an email to a user on his birthday, on daily basis, but wp_schedule is not working. I want to send the email with a coupon on his birthday.
add_action(int) hook is working fine, it creates the coupon and sends the mail but with schedule it is not working. How can I compare date of birth of the user with the current date and send the coupon?
public function user_checked_date_create_coupan() {
global $woocommerce;
// how many time send main
$coupon_send_times = 1;
//today date
$today = date("Y-m-d");
// get name of store user
$store_name = get_bloginfo('name');
// store url
$store_url = get_bloginfo('url');
// send mail before days
$mail_before_days = get_option('coupan_before_days');
// condition main before
if( $mail_before_days == 0 || empty($mail_before_days) )
$mail_before_days = 0;
/// get field date
$args = array(
'meta_key' => 'dobc_date_field',
);
$birthday_users = get_users($args);
if( is_array($birthday_users) && !empty($birthday_users) ) {
foreach( $birthday_users as $birthday_user ) {
$get_user_birthday = get_user_meta($birthday_user->data->ID, 'dobc_date_field', true );
if( !empty($get_user_birthday) ) {
$today = date("Y-m-d");
$new_birthday_year = date('Y').'-'.$get_user_birthday;
$date = date('Y-m-d', strtotime($new_birthday_year . " -".$mail_before_days." days"));
// print_r($date);
// exit();
if( $today ==$get_user_birthday) {
$user_info = get_userdata($birthday_user->data->ID);
//check user is eligible for send birthday this time
$check_coupon_year = get_user_meta($birthday_user->data->ID, 'coupon_year', true );
if( empty($check_coupon_year) || $check_coupon_year == '' ) {
$check_coupon_year = 0;
}
$check_coupon_year = intval($check_coupon_year);
if( $coupon_send_times == 0 || $check_coupon_year <= $coupon_send_times ) {
$user_email = $birthday_user->data->user_email;
$username = $birthday_user->data->user_login;
if( !empty($user_email) ) {
//create coupon based on settings
$code_length = get_option( 'dobc_code_length' );
if( $code_length == '' )
$code_length = 12;
$prefix = get_option( 'dobc_prefix' );
$code = $prefix . strtoupper( substr( str_shuffle( md5( time() ) ), 0, $code_length ) );
$type = get_option( 'dobc_dis_type' );
$amount = get_option( 'dobc_amount' );
$product_ids = get_option( 'select_product' );
$allowed_products = '';
if ( is_array( $product_ids ) ) {
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
$allowed_products .= ''.$product->get_title().',';
}
$allowed_products = rtrim( $allowed_products, ',' );
$product_ids = implode( ',', $product_ids );
}
$product_categories = get_option( 'dobc_categories' );
$allowed_cats = '';
if ( is_array( $product_categories ) ) {
foreach ( $product_categories as $cat_id ) {
$cat = get_term_by( 'id', $cat_id, 'product_cat' );
$allowed_cats .='</br>'. ''.$cat->name.','.'</br>';
}
$allowed_cats = rtrim( $allowed_cats, ',' );
////
///
}
else
$product_categories = array();
$days = get_option( 'dobc_days' );
$date = '';
$expire = '';
$format = get_option( 'dobc_date_format' ) == '' ? 'jS F Y' : get_option( 'dobc_date_format' );
if ( $days ) {
$date = date( 'Y-m-d', strtotime( '+'.$days.' days' ) );
$expire = date_i18n( $format, strtotime( '+'.$days.' days' ) );
}
$free_shipping = get_option( 'dobc_shipping' );
$exclude_sale_items = get_option( 'dobc_sale' );
$minimum_amount = get_option( 'dobc_min_purchas' );
$maximum_amount = get_option( 'dobc_max_purchase' );
$customer_email = '';
if ( get_option( 'dobc_restrict' ) == 'yes' )
$customer_email = $user_email;
//Add a new coupon when user registers
$coupon = array(
'post_title' => $code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$coupon_id = wp_insert_post( $coupon );
//Add coupon meta data
update_post_meta( $coupon_id, 'discount_type', $type );
update_post_meta( $coupon_id, 'coupon_amount', $amount );
update_post_meta( $coupon_id, 'individual_use', 'yes' );
update_post_meta( $coupon_id, 'product_ids', $product_ids );
update_post_meta( $coupon_id, 'usage_limit', '1' );
update_post_meta( $coupon_id, 'usage_limit_per_user', '1' );
update_post_meta( $coupon_id, 'limit_usage_to_x_items', '' );
update_post_meta( $coupon_id, 'expiry_date', $date );
update_post_meta( $coupon_id, 'apply_before_tax', 'no' );
update_post_meta( $coupon_id, 'free_shipping', $free_shipping );
update_post_meta( $coupon_id, 'exclude_sale_items', $exclude_sale_items );
update_post_meta( $coupon_id, 'product_categories', $product_categories );
update_post_meta( $coupon_id, 'minimum_amount', $minimum_amount );
update_post_meta( $coupon_id, 'maximum_amount', $maximum_amount );
update_post_meta( $coupon_id, 'customer_email', $customer_email );
$search = array( '{COUPONCODE}', '{COUPONEXPIRY}', '{ALLOWEDCATEGORIES}', '{ALLOWEDPRODUCTS}', '{STORENAME}', '{STOREURL}');
$replace = array( $code, $expire, $allowed_cats, $allowed_products,$username,
$store_name, $store_url );
$subject = str_replace( $search, $replace, get_option( 'dobc_email_sub' ) );
$body = str_replace( $search, $replace, get_option( 'email_body_id' ) );
// $body = "<html><body><h1 class ='test'>( $body ); </h1></body></html>";
$body = "<html><body><div style ='padding:10px 80px;'>( $body ); </div</body></html>";
$user_id = get_current_user_id();
$user_info = get_userdata($user_id);
$to = $user_info->user_email;
//////
$message= $this->dobc_email_template( esc_html__( '', 'bobc' ) . get_option( 'dobc_email_sub'), $body );
// wp_mail( $user_email, esc_html( __(get_option( 'dobc_email_sub'), 'bobc' ) ), $body, $headers );
// print_r($body);
$headers = array('Content-Type: text/html; charset=UTF-8');
//////////////////
wp_mail( $to, $subject, $message, $headers );
//mil send valid or mnoy
//$sent = wp_mail($to, $subject, strip_tags($body), $headers);
$new_coupon_year = $check_coupon_year + 1;
update_user_meta($birthday_user->data->ID, 'coupon_year', $new_coupon_year );
}
}
}
}
}
}
}
Finally i did it, I'm using "Front-End PM" plugin, So far I created this function to send message for the post Author when he/she post is published.
But how can i create two messages one for the post author and one for all users, for notice all of them about the new published post.
add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );
function fep_cus_user_publish_send_messaage( $ID, $post ){
if ( ! function_exists( 'fep_send_message' ) )
return;
$message = [];
$message['message_to_id'] = $post->post_author; // Post author ID.
$name = get_the_author_meta( 'display_name', $post->post_author );
$title = $post->post_title;
$permalink = get_permalink( $ID );
$message['message_title'] = sprintf( 'Published: %s', $title );
$message['message_content'] = sprintf ('Congratulations, %s! Your article “%s” has been published.', $name, $title );
$message['message_content'] .= sprintf( 'View: %s', $permalink );
$message['message_content'] .= sprintf( 'This is an automatic message, to let you know your post is published, and qualified for our quality standard!' );
$override = array('post_author' => 1);//change with message sender id
// Send message
fep_send_message( $message, $override );
}
Here is the fep_send_message function, I want to make the two message in one function..this can happen?
function fep_send_message( $message = null, $override = array() ) {
if ( null === $message ) {
$message = $_POST;
}
if ( ! empty( $message['fep_parent_id'] ) ) {
$message['post_parent'] = absint( $message['fep_parent_id'] );
$message['post_status'] = fep_get_option( 'reply_post_status', 'publish' );
$message['message_title'] = __( 'RE:', 'front-end-pm' ). ' ' . wp_slash( fep_get_message_field( 'mgs_title', $message['post_parent'] ) );
$message['message_to_id'] = fep_get_participants( $message['post_parent'], true );
} else {
$message['post_status'] = fep_get_option( 'parent_post_status','publish' );
$message['post_parent'] = 0;
}
$message = apply_filters( 'fep_filter_message_before_send', $message );
if ( empty( $message['message_title'] ) || empty( $message['message_content'] ) ) {
return false;
}
// Create post array
$post = array(
'post_title' => $message['message_title'],
'post_content' => $message['message_content'],
'post_status' => $message['post_status'],
'post_parent' => $message['post_parent'],
'post_type' => 'message',
'post_author' => get_current_user_id(),
'mgs_created' => current_time( 'mysql', true ),
);
if ( $override && is_array( $override ) ) {
$post = wp_parse_args( $override, $post );
}
if( ! $post['post_parent'] && 'threaded' === fep_get_message_view() ){
$post['mgs_last_reply_by
'] = $post['post_author'];
$post['mgs_last_reply_excerpt'] = fep_get_the_excerpt_from_content( 100, $post['post_content'] );
$post['mgs_last_reply_time'] = $post['mgs_created'];
}
$post = apply_filters( 'fep_filter_message_after_override', $post, $message );
foreach( $post as $k => $v ){
if( 0 === strpos( $k, 'post_') ){
$post[ str_replace( 'post_', 'mgs_', $k ) ] = $v;
unset( $post[ $k ] );
}
}
$post = wp_unslash( $post );
$new_message = new FEP_Message;
$message_id = $new_message->insert( $post );
// Insert the message into the database
if ( ! $message_id ) {
return false;
}
/*
$inserted_message = FEP_Message::get_instance( $message_id );
if( ! $inserted_message ){
return false;
}
*/
if( ! empty( $message['message_to_id'] ) ){
$message['message_to_id'] = (array) $message['message_to_id'];
$message['message_to_id'][] = $new_message->mgs_author;
$new_message->insert_participants( $message['message_to_id'] );
}
do_action( 'fep_action_message_after_send', $message_id, $message, $new_message );
fep_status_change( 'new', $new_message );
return $message_id;
}
Please check below code for Front End PM plugin. I updated some code in "send_message_to_contributor" as "fep_send_message".
add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );
function fep_cus_user_publish_send_messaage( $ID, $post ){
if ( ! function_exists( 'fep_send_message' ) )
return;
$message = [];
$message['message_to_id'] = $post->post_author; // Post author ID.
$name = get_the_author_meta( 'display_name', $post->post_author );
$title = $post->post_title;
$permalink = get_permalink( $ID );
$message['message_title'] = sprintf( 'Published: %s', $title );
$message['message_content'] = sprintf ('Congratulations, %s! Your article “%s” has been published.', $name, $title );
$message['message_content'] .= sprintf( 'View: %s', $permalink );
$message['message_content'] .= sprintf( 'This is an automatic message, to let you know your post is published, and qualified for our quality standard!' );
$override = array('post_author' => 1);//change with message sender id
// Send message
send_message_to_contributor( $name, $title );
}
function send_message_to_contributor( $message = null, $override = array() ) {
$blogusers = get_users();
if ( null === $message ) {
$message = $_POST;
}
if ( ! empty( $message['fep_parent_id'] ) ) {
$message['post_parent'] = absint( $message['fep_parent_id'] );
$message['post_status'] = fep_get_option( 'reply_post_status', 'publish' );
$message['message_title'] = __( 'RE:', 'front-end-pm' ). ' ' . wp_slash( fep_get_message_field( 'mgs_title', $message['post_parent'] ) );
$message['message_to_id'] = fep_get_participants( $message['post_parent'], true );
} else {
$message['post_status'] = fep_get_option( 'parent_post_status','publish' );
$message['post_parent'] = 0;
}
$message = apply_filters( 'fep_filter_message_before_send', $message );
if ( empty( $message['message_title'] ) || empty( $message['message_content'] ) ) {
return false;
}
// Create post array
$post = array(
'post_title' => $message['message_title'],
'post_content' => $message['message_content'],
'post_status' => $message['post_status'],
'post_parent' => $message['post_parent'],
'post_type' => 'message',
'post_author' => get_current_user_id(),
'mgs_created' => current_time( 'mysql', true ),
);
if ( $override && is_array( $override ) ) {
$post = wp_parse_args( $override, $post );
}
if( ! $post['post_parent'] && 'threaded' === fep_get_message_view() ){
$post['mgs_last_reply_by'] = $post['post_author'];
$post['mgs_last_reply_excerpt'] = fep_get_the_excerpt_from_content( 100, $post['post_content'] );
$post['mgs_last_reply_time'] = $post['mgs_created'];
}
$post = apply_filters( 'fep_filter_message_after_override', $post, $message );
foreach( $post as $k => $v ){
if( 0 === strpos( $k, 'post_') ){
$post[ str_replace( 'post_', 'mgs_', $k ) ] = $v;
unset( $post[ $k ] );
}
}
$post = wp_unslash( $post );
$new_message = new FEP_Message;
$message_id = $new_message->insert( $post );
// Insert the message into the database
if ( ! $message_id ) {
return false;
}
/*
$inserted_message = FEP_Message::get_instance( $message_id );
if( ! $inserted_message ){
return false;
}
*/
/*if( ! empty( $message['message_to_id'] ) ){
$message['message_to_id'] = (array) $message['message_to_id'];
$message['message_to_id'][] = $new_message->mgs_author;
$new_message->insert_participants( $message['message_to_id'] );
}*/
/*Update code here*/
foreach ($blogusers as $key => $value) {
$message['message_to_id'] = array();
$message_user = $value->data->ID;
if($message_user != $new_message->mgs_author){
$message['message_to_id'][] = $message_user;//(array) $message['message_to_id'];
$message['message_to_id'][] = $new_message->mgs_author;
$new_message->insert_participants( $message['message_to_id'] );
}
}
/*End updated code*/
do_action( 'fep_action_message_after_send', $message_id, $message, $new_message );
fep_status_change( 'new', $new_message );
return $message_id;
}
Please check below code. It will help you.
add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );
function fep_cus_user_publish_send_messaage( $ID, $post ){
$author = $post->post_author; /* Post author ID. */
$name = get_the_author_meta( 'display_name', $author );
$email = get_the_author_meta( 'user_email', $author );
$title = $post->post_title;
$permalink = get_permalink( $ID );
$edit = get_edit_post_link( $ID, '' );
$to[] = sprintf( '%s <%s>', $name, $email );
$subject = sprintf( 'Published: %s', $title );
$messaged = sprintf ('Congratulations, %s! Your article “%s” has been published.' . "\n\n", $name, $title );
$messaged .= sprintf( 'View: %s', $permalink );
$headers[] = '';
wp_mail( $to, $subject, $messaged, $headers );
// Send message
send_message_to_contributor( $name, $title );
}
function send_message_to_contributor($author_name, $post_title) {
$blogusers = get_users( [ 'role__in' => [ 'contributor' ] ] );
foreach ( $blogusers as $user ) {
$permalink = get_permalink( $ID );
$user_name = $user->data->display_name;
$email = $user->data->user_email;
$to[] = sprintf( '%s <%s>', $user_name, $email );
$subject = sprintf( 'Published: %s', $post_title );
$message = sprintf ('Hello, %s! New post is “%s” published.' . "\n\n", $user_name, $post_title );
$message .= sprintf( 'View: %s', $permalink );
$headers[] = '';
wp_mail( $to, $subject, $message, $headers );
}
}
If email is not sent then install this plugin and set up SMTP Easy WP SMTP
i have a shortcode that posts recent blog entries from a certain category on one of my web pages, however i want to display a static link at the end of everypost, is there anyway to do this?
the following code is used to display the posts:
<?php echo do_shortcode('[display-posts category="competitions" posts_per_page="4" include_excerpt="true" image_size="thumbnail" wrapper="ul"]');
Thanks in advance.
<?php
// Create the shortcode
add_shortcode( 'display-posts', 'be_display_posts_shortcode' );
function be_display_posts_shortcode( $atts ) {
// Original Attributes, for filters
$original_atts = $atts;
// Pull in shortcode attributes and set defaults
$atts = shortcode_atts( array(
'title' => '',
'author' => '',
'category' => '',
'date_format' => '(n/j/Y)',
'display_posts_off' => false,
'exclude_current' => false,
'id' => false,
'ignore_sticky_posts' => false,
'image_size' => false,
'include_title' => true,
'include_author' => false,
'include_content' => false,
'include_date' => false,
'include_excerpt' => false,
'meta_key' => '',
'meta_value' => '',
'no_posts_message' => '',
'offset' => 0,
'order' => 'DESC',
'orderby' => 'date',
'post_parent' => false,
'post_status' => 'publish',
'post_type' => 'post',
'posts_per_page' => '10',
'tag' => '',
'tax_operator' => 'IN',
'tax_term' => false,
'taxonomy' => false,
'wrapper' => 'ul',
'wrapper_class' => 'display-posts-listing',
'wrapper_id' => false,
), $atts, 'display-posts' );
// End early if shortcode should be turned off
if( $atts['display_posts_off'] )
return;
$shortcode_title = sanitize_text_field( $atts['title'] );
$author = sanitize_text_field( $atts['author'] );
$category = sanitize_text_field( $atts['category'] );
$date_format = sanitize_text_field( $atts['date_format'] );
$exclude_current = be_display_posts_bool( $atts['exclude_current'] );
$id = $atts['id']; // Sanitized later as an array of integers
$ignore_sticky_posts = be_display_posts_bool( $atts['ignore_sticky_posts'] );
$image_size = sanitize_key( $atts['image_size'] );
$include_title = be_display_posts_bool( $atts['include_title'] );
$include_author = be_display_posts_bool( $atts['include_author'] );
$include_content = be_display_posts_bool( $atts['include_content'] );
$include_date = be_display_posts_bool( $atts['include_date'] );
$include_excerpt = be_display_posts_bool( $atts['include_excerpt'] );
$meta_key = sanitize_text_field( $atts['meta_key'] );
$meta_value = sanitize_text_field( $atts['meta_value'] );
$no_posts_message = sanitize_text_field( $atts['no_posts_message'] );
$offset = intval( $atts['offset'] );
$order = sanitize_key( $atts['order'] );
$orderby = sanitize_key( $atts['orderby'] );
$post_parent = $atts['post_parent']; // Validated later, after check for 'current'
$post_status = $atts['post_status']; // Validated later as one of a few values
$post_type = sanitize_text_field( $atts['post_type'] );
$posts_per_page = intval( $atts['posts_per_page'] );
$tag = sanitize_text_field( $atts['tag'] );
$tax_operator = $atts['tax_operator']; // Validated later as one of a few values
$tax_term = sanitize_text_field( $atts['tax_term'] );
$taxonomy = sanitize_key( $atts['taxonomy'] );
$wrapper = sanitize_text_field( $atts['wrapper'] );
$wrapper_class = sanitize_html_class( $atts['wrapper_class'] );
if( !empty( $wrapper_class ) )
$wrapper_class = ' class="' . $wrapper_class . '"';
$wrapper_id = sanitize_html_class( $atts['wrapper_id'] );
if( !empty( $wrapper_id ) )
$wrapper_id = ' id="' . $wrapper_id . '"';
// Set up initial query for post
$args = array(
'category_name' => $category,
'order' => $order,
'orderby' => $orderby,
'post_type' => explode( ',', $post_type ),
'posts_per_page' => $posts_per_page,
'tag' => $tag,
);
// Ignore Sticky Posts
if( $ignore_sticky_posts )
$args['ignore_sticky_posts'] = true;
// Meta key (for ordering)
if( !empty( $meta_key ) )
$args['meta_key'] = $meta_key;
// Meta value (for simple meta queries)
if( !empty( $meta_value ) )
$args['meta_value'] = $meta_value;
// If Post IDs
if( $id ) {
$posts_in = array_map( 'intval', explode( ',', $id ) );
$args['post__in'] = $posts_in;
}
// If Exclude Current
if( $exclude_current )
$args['post__not_in'] = array( get_the_ID() );
// Post Author
if( !empty( $author ) )
$args['author_name'] = $author;
// Offset
if( !empty( $offset ) )
$args['offset'] = $offset;
// Post Status
$post_status = explode( ', ', $post_status );
$validated = array();
$available = array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash', 'any' );
foreach ( $post_status as $unvalidated )
if ( in_array( $unvalidated, $available ) )
$validated[] = $unvalidated;
if( !empty( $validated ) )
$args['post_status'] = $validated;
// If taxonomy attributes, create a taxonomy query
if ( !empty( $taxonomy ) && !empty( $tax_term ) ) {
// Term string to array
$tax_term = explode( ', ', $tax_term );
// Validate operator
if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
$tax_operator = 'IN';
$tax_args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $tax_term,
'operator' => $tax_operator
)
)
);
// Check for multiple taxonomy queries
$count = 2;
$more_tax_queries = false;
while(
isset( $original_atts['taxonomy_' . $count] ) && !empty( $original_atts['taxonomy_' . $count] ) &&
isset( $original_atts['tax_' . $count . '_term'] ) && !empty( $original_atts['tax_' . $count . '_term'] )
):
// Sanitize values
$more_tax_queries = true;
$taxonomy = sanitize_key( $original_atts['taxonomy_' . $count] );
$terms = explode( ', ', sanitize_text_field( $original_atts['tax_' . $count . '_term'] ) );
$tax_operator = isset( $original_atts['tax_' . $count . '_operator'] ) ? $original_atts['tax_' . $count . '_operator'] : 'IN';
$tax_operator = in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) ? $tax_operator : 'IN';
$tax_args['tax_query'][] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms,
'operator' => $tax_operator
);
$count++;
endwhile;
if( $more_tax_queries ):
$tax_relation = 'AND';
if( isset( $original_atts['tax_relation'] ) && in_array( $original_atts['tax_relation'], array( 'AND', 'OR' ) ) )
$tax_relation = $original_atts['tax_relation'];
$args['tax_query']['relation'] = $tax_relation;
endif;
$args = array_merge( $args, $tax_args );
}
// If post parent attribute, set up parent
if( $post_parent ) {
if( 'current' == $post_parent ) {
global $post;
$post_parent = get_the_ID();
}
$args['post_parent'] = intval( $post_parent );
}
// Set up html elements used to wrap the posts.
// Default is ul/li, but can also be ol/li and div/div
$wrapper_options = array( 'ul', 'ol', 'div' );
if( ! in_array( $wrapper, $wrapper_options ) )
$wrapper = 'ul';
$inner_wrapper = 'div' == $wrapper ? 'div' : 'li';
$listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $original_atts ) );
if ( ! $listing->have_posts() )
return apply_filters( 'display_posts_shortcode_no_results', wpautop( $no_posts_message ) );
$inner = '';
while ( $listing->have_posts() ): $listing->the_post(); global $post;
$image = $date = $author = $excerpt = $content = '';
if ( $include_title )
$title = '<a class="title" href="' . apply_filters( 'the_permalink', get_permalink() ) . '">' . get_the_title() . '</a>';
if ( $image_size && has_post_thumbnail() )
$image = '<a class="image" href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), $image_size ) . '</a> ';
if ( $include_date )
$date = ' <span class="date">' . get_the_date( $date_format ) . '</span>';
if( $include_author )
$author = apply_filters( 'display_posts_shortcode_author', ' <span class="author">by ' . get_the_author() . '</span>' );
if ( $include_excerpt )
$excerpt = ' <span class="excerpt-dash">-</span> <span class="excerpt">' . get_the_excerpt() . '</span>';
if( $include_content ) {
add_filter( 'shortcode_atts_display-posts', 'be_display_posts_off', 10, 3 );
$content = '<div class="content">' . apply_filters( 'the_content', get_the_content() ) . '</div>';
remove_filter( 'shortcode_atts_display-posts', 'be_display_posts_off', 10, 3 );
}
$class = array( 'listing-item' );
$class = sanitize_html_class( apply_filters( 'display_posts_shortcode_post_class', $class, $post, $listing, $original_atts ) );
$output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $author . $excerpt . $content . '</' . $inner_wrapper . '>';
// If post is set to private, only show to logged in users
if( 'private' == get_post_status( get_the_ID() ) && !current_user_can( 'read_private_posts' ) )
$output = '';
$inner .= apply_filters( 'display_posts_shortcode_output', $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class );
endwhile; wp_reset_postdata();
$open = apply_filters( 'display_posts_shortcode_wrapper_open', '<' . $wrapper . $wrapper_class . $wrapper_id . '>', $original_atts );
$close = apply_filters( 'display_posts_shortcode_wrapper_close', '</' . $wrapper . '>', $original_atts );
$return = $open;
if( $shortcode_title ) {
$title_tag = apply_filters( 'display_posts_shortcode_title_tag', 'h2', $original_atts );
$return .= '<' . $title_tag . ' class="display-posts-title">' . $shortcode_title . '</' . $title_tag . '>' . "\n";
}
$return .= $inner . $close;
return $return;
}
/**
* Turn off display posts shortcode
* If display full post content, any uses of [display-posts] are disabled
*
* #param array $out, returned shortcode values
* #param array $pairs, list of supported attributes and their defaults
* #param array $atts, original shortcode attributes
* #return array $out
*/
function be_display_posts_off( $out, $pairs, $atts ) {
$out['display_posts_off'] = true;
return $out;
}
/**
* Convert string to boolean
* because (bool) "false" == true
*
*/
function be_display_posts_bool( $value ) {
return !empty( $value ) && 'true' == $value ? true : false;
}
You'll want to edit you $output variable which is on line 243 on the code you've given above.
A simple amendment to add a static url will do fine, something like this:
$static_link = 'http://www.test.com/test';
$output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $author . $excerpt . $content . 'Read more' . '</' . $inner_wrapper . '>';
Amend this to your requirements, say adding a proper link from the database.
Hope this helps.
I created a plugin and create a menu by using this code
add_filter('page_template', 'in_page_template');
function in_page_template()
{
global $wpdb;
$new_page_title = 'Packages';
$sql = "SELECT * FROM wp_posts where post_name='" . $new_page_title . "';";
$cnt_post = $wpdb->get_results($sql);
if (!(is_page('Home'))) {
$ppid = $_GET['page_id'];
if (count($cnt_post) != 0) {
$pid = $cnt_post[0]->ID;
if ($pid == $ppid) {
$page_template = dirname(__FILE__) . '/Packages.php';
return $page_template;
}
}
}
}
How can I create submenu for package page
add_action('init', 'create_initial_pages');
function create_initial_pages() {
$pages = array(
array(
'name' => 'post_name',
'title' => 'post_title',
'child' => array(
'page1-1' => 'National',
'page1-2' => 'International'
)
),
);
$template = array(
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1
);
foreach( $pages as $page ) {
$exists = get_page_by_title( $page['title'] );
$my_page = array(
'post_name' => $page['name'],
'post_title' => $page['title']
);
$my_page = array_merge( $my_page, $template );
$id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );
if( isset( $page['child'] ) ) {
foreach( $page['child'] as $key => $value ) {
$child_id = get_page_by_title( $value );
$child_page = array(
'post_name' => $key,
'post_title' => $value,
'post_parent' => $id
);
$child_page = array_merge( $child_page, $template );
if( !isset( $child_id ) ) wp_insert_post( $child_page );
}
}
}
}