When the post is publish, I want the publish date as the same as the date in the meta data, imic_sermon_date.
Here's my code. The problem is that it get stuck when publishing a post.
function update_sermon_date ($post_id) {
//automatically change publish date to sermon date when publish/save a postfunction update_sermon_date ($post_id) {
$sermon_date = get_post_meta($post_id, 'imic_sermon_date', true);
$mypost = array (
'ID' => $post_id,
'post_date' => $sermon_date);
wp_update_post( $mypost );
}
add_action ('save_post', 'update_sermon_date');
Can someone help me with this?
Thanks.
After searching online, I've fixed my code.
The code basically will only update when the post is edited. I added remove_action and add_action in order to prevent the code from running an endless loop.
****//automatically change publish date to sermon date when publish/save a post
function update_sermon_date () {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if ( !current_user_can('edit_post', $post_id) )
return;
$sermon_date = get_post_meta(get_the_ID(), 'imic_sermon_date', true);
$mypost = array (
'ID' => get_the_ID(),
'post_title' => $sermon_date,
'post_date' => $sermon_date,
'post_date_gmt' => $sermon_date);
if ( ! (wp_is_post_revision($post_id) || wp_is_post_autosave ( $post_id) ) ) {
remove_action('edit_post','update_sermon_date');
wp_update_post( $mypost );
add_action ('edit_post', 'update_sermon_date');
}
}
add_action ('edit_post', 'update_sermon_date');****
Related
I have a custom date field, added with ACF, for each of my products at which point the product status should change to draft.
I know there are a bunch of schedular and count time plugins, but it comes with a bell and whistle I don't need. Is there a simple way to achieve this
Thank you
You can use WorsPress CRON. You can use wp_schedule_event. You have to get all products and get your ACF field to compare against today or current date then use the wp_update_post function to update posts status. try the below code.
// 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' => 'publish'
);
$get_products = new WP_Query( $args );
if( $get_products->have_posts() ){ while ( $get_products->have_posts() ) { $get_products->the_post();
$draft_date = strtotime( get_field('keyname', get_the_ID() ) );
$current_date = time();
if( $current_date >= $draft_date ){
$my_post = array(
'ID' => get_the_ID(),
'post_status' => 'draft',
);
wp_update_post( $my_post );
}
} wp_reset_postdata(); }
}
I'm trying to update the sorting of search results in wordpress / woocommerce
so, when there is a search as: mydomain.com/?s=cases&post_type=product
The results get sorted by the 'total_sale' meta_key
I have the following hook
function sv_update_default_search_to_sales( $query ){
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || ! $query->is_main_query() ){
return;
}
// Query Update for Search Results pages with products
if (is_search() && is_woocommerce() ){
if(get_query_var('post_type') != 'product'){
return;
}
$orderBy = get_query_var('orderby');
if(empty($orderBy) || $orderBy == 'relevance'){
$query->set('orderby', 'meta_value_num');
$query->set('order', 'desc');
$query->set('meta_key', 'total_sales');
$query->set('meta_type', 'NUMERIC');
}
}
}
add_action( 'pre_get_posts', 'sv_update_default_search_to_sales', 1 );
When I output $wp_query after the query has been parsed, i can see the 'meta_key' has been set to blank.
[meta_key] =>
[orderby] => meta_value_num
[order] => DESC
[meta_type] => NUMERIC
I've seen on other codes examples where they set meta_key and meta_value for matching; however, i don't have a set value, i need to order the results by 'total_sales' meta key.
Im basically trying to recreate this type of query call:
$query = new WP_Query([
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num'
]);
OK, so after further debugging. I realized there were other plugins modifying the query that were set to fire after my action.
I don't know which plugin specifically was modifying it, but after the page is rendered I printed the filter with
global $wp_filter;
echo '<pre>';
print_r($wp_filter['pre_get_posts']);
echo '</pre>';
Figured outthe action number: 90000 was the last action hooked, so I set mines to 90001
add_action( 'pre_get_posts', 'sv_update_default_search_to_sales', 900001 );
and it worked! x)
I am using a custom plugin to add previous and next links in the "admin edit post" page. This so that the author can easily skip from his own posts (previous to next) without having to leave that page in the backend.
I am however struggling with how I need to get there.
Is the below adjustable to make it display only the current editing user ?
function change_apn_post_status( $post_statuses, $post_type ) {
// Add a post status.
// Note: by default these are already in the $post_statuses array: 'draft', 'future', 'pending', 'private', 'publish'
$post_statuses[] = 'trash';
// Remove post status(es).
$post_statuses_to_remove = array( 'draft' ); // Customize here.
if ( 'page' === $post_type ) {
$post_statuses_to_remove[] = 'pending';
}
foreach ( $post_statuses_to_remove as $remove ) {
if ( false !== $index = array_search( $remove, $post_statuses ) ) {
unset( $post_statuses[ $index ] );
}
}
return array_values( $post_statuses );
}
add_filter( 'c2c_admin_post_navigation_post_statuses', 'change_apn_post_status', 10, 2 );'
I thought this was a good starting point but alas I can't get it to work
global $current_user;
wp_get_current_user();
$author_query = array(
'posts_per_page' => '-1',
'author' => $current_user->ID
);
I have a gravity form form on my WP site and I recently changed a free text field into a drop down field.
The website is a store which hold several categories of goods and I want my drop-down to show the user all the possible categories he can choose from.
Please assist in how to "pull" the categories into the drop-down list.
Thanks in advance.
You can do using some filters of gravity form, code is following
// Here 1 is form id
add_filter( 'gform_pre_render_1', 'populate_category' );
add_filter( 'gform_pre_validation_1', 'populate_category' );
add_filter( 'gform_pre_submission_filter_1', 'populate_category' );
add_filter( 'gform_admin_pre_render_1', 'populate_category' );
function populate_category( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-category' ) === false ) {
continue;
}
// Get category list
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$choices = array();
foreach( $categories as $category ) {
$choices[] = array( 'text' => $category->name, 'value' => $category->name );
}
$field->placeholder = 'Select a Category';
$field->choices = $choices;
}
return $form;
}
This is working perfectly its tested code.
It appears as tho the search functionality for WooCommerce products does not check "product_tag" taxonomy terms, nor SKU field? I added the SKUs as product tags to their respective products, but it still returns nothing when I search for the SKU.... How do I make the search functionality check product_tag terms? I have tried many many many things from adding tax_query to pre_get_post filter, to a whole new WP_Query loop, it just fails to search product_tags for some reason....so what is the point in a Product Tag???
I've tried the code from woo and others to search SKU with no results. However, the plugin search-everything, download from repository, does the job.
I got the solution to search the products by SKU code.
It is too simple just paste the following code in function.php and then you can see the changes in search results.
function search_by_id_only( $search, &$query_vars ) {
global $wpdb, $pagenow;
if ( 'edit.php' == $pagenow || empty($search) ) {
return $search;
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_sku',
'value' => $query_vars->query['s'],
'compare' => 'LIKE'
)
)
);
$posts = get_posts( $args );
if ( empty( $posts ) ) return $search;
$get_post_ids = array();
foreach($posts as $post){
$get_post_ids[] = $post->ID;
}
if ( sizeof( $get_post_ids ) > 0 ) {
$search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
}
return $search;
}
add_filter( 'posts_search', 'search_by_id_only', 999, 2 );