I am making a function in a plugin and the function will delete the database row when the post move to the trash bin. However, I cannot get the post_id using get_posts().
Here is my code:
function delete_condition($post)
{
global $wpdb;
$allposts = get_posts(array(
'numberposts' => -1,
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'job',
'suppress_filters' => true));
foreach( $allposts as $postinfo ) {
$wpdb->delete('rule', array('post_id' => $postinfo));
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
thanks
The action hook you're using here, wp_trash_post, passes the $post_id to the function as a parameter. See: https://codex.wordpress.org/Plugin_API/Action_Reference/trash_post
It sounds like you want to delete all rows from one table that have the same post ID as the one being trashed.
I think you might want to write something like this:
function delete_condition( $post_id ) {
global $wpdb;
// Delete rows in the rule table which have the same post_id as this one
if ( 'job' === get_post_type( $post_id ) ) {
$wpdb->delete('rule', array('post_id' => $post_id ) );
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
$postinfo is the object. You want only the ID of post. So you should write $postinfo->ID. Replace your loop with the below -
foreach( $allposts as $postinfo ) {
$postinfoID = $postinfo->ID;
$wpdb->delete('rule', array('post_id' => $postinfoID));
}
<?php
function delete_condition($post)
{
global $wpdb;
$allposts = get_posts(array(
'numberposts' => -1,
'post_status' => 'any',
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'job',
'suppress_filters' => true));
foreach( $allposts as $postinfo ) {
$wpdb->delete('rule', array('post_id' => $postinfo));
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
?>
Related
Hi all I'm trying to ad a meta_query to my wp query args.
Here is my post meta:
$post_options = get_post_meta( get_the_ID(), 'post_options')[0];
print_r($post_options);
which produces an array like this:
Array ( [on_demand] => true [featured_video] => https://example.com/1234 )
Here is my code, the normal loop works fine but breaks when I add the meta_query:
//helper function from https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'post_options_%", "meta_key LIKE 'post_options_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'post_options_%_on_demand',
'value' => 'true',
'compare' => '='
)
)
);
// The Query
$the_query = new WP_Query( $args );
I am seeking a way how to delete product variations by filtering phrases in the variations title.
I use this post filter:
case 'trash_unwanted_products':
$args = array(
'post_type' => array('product','product_variation'),
'post_status' => array('publish','draft'),
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'fields' => 'ids',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'asc',
);
break;
And this is my set of post trashing functions:
private function trash_unwanted_products($product) {
if (!$product->exists()) {
return false;
}
$productName = $product->get_name();
$productName_slug = sanitize_title($productName);
$productName_dash = sanitize_title_with_dashes($productName);
$productSKU = $product->get_sku();
if ($product->is_type( 'variable' )) {
$variation = new WC_Product_Variation($variation_id);
$title_slug = current($variation->get_variation_attributes());
$results = $wpdb->get_results("SELECT * FROM wp_terms WHERE slug = '{$title_slug}'", ARRAY_A);
$variationName = $results[0]['name'];
$variationName_slug = sanitize_title($variationName);
$variationName_dash = sanitize_title_with_dashes($variationName);
}
if (strpos($productName_slug,'ubrus')!==false && strpos($productName_slug,'vanocni')!==false) {
$this->trash_product($product);
}
if ($product->is_type( 'variable' ) && strpos($variationName_slug,'latka')!==false && strpos($variationName_dash,'cena-za-1-bezny-metr')!==false) {
$this->wp_delete_post($variation->get_id(), true);
}
}
private function trash_product($product) {
wp_delete_post($product->get_id(), true);
}
However, I am not sure if the script goes through all variations in each variable product and thus has the ability to delete the targeted product variations.
Any suggestions?
I have developed my own filter for variations, but I havenĀ“t had the opportunity to test it yet.
case 'trash_unwanted_variations':
$args = array(
'post_type' => 'product_variation',
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'fields' => 'ids',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'asc',
's' => get_search_query(),
'title' => array(
array(
'value' => sanitize_title_with_dashes('cena-za-1-bezny-metr'),
'compare' => 'IN',
)
),
);
break;
Could this perhaps solve the question? (if is going to work) :D..
I want to add conditional statement in author. something like if the author is an admin set this to null. is this possible?
$args = apply_filters( 'job_manager_get_dashboard_jobs_args', array(
'post_type' => 'job_listing',
'post_status' => array( 'publish', 'expired', 'pending' ),
'ignore_sticky_posts' => 1,
'posts_per_page' => $posts_per_page,
'offset' => ( max( 1, get_query_var('paged') ) - 1 ) * $posts_per_page,
'orderby' => 'date',
'order' => 'desc',
'author' => get_current_user_id()
));
I assume that get_current_user_id() is getting an id for author.
Change your code from this:
'author' => get_current_user_id()
With this:
'author' => ((get_current_user_id() == 'admin') ? NULL : get_current_user_id())
I think you can use this sample code.
<?php
$array = array('test' => testFunction());
function testFunction()
{
// do your condition here
// return 'a';
}
print_r($array);
?>
Is it possible to get an array of all wordpress child page IDs that are no using a certain template?
I know I can get the template with:
'meta_key' => '_wp_page_template',
'meta_value' => 'template.php'
and I expect I could use get_pages, but not sure how I can put the query together.
This works, but is there a better way of doing it?
$args = array(
'post_parent' => $parent_ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
);
$children = get_children( $args );
$exclude_children = null;
foreach($children as $child) {
$metadata = get_post_meta($child->ID);
if($metadata['_wp_page_template'][0] == 'template.php') {
$args = array(
'post_parent' => $child->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
);
$exclude_array = get_children( $args );
foreach($exclude_array as $exclude) {
$exclude_children .= $exclude->ID.',';
}
}
}
I have kept a Prev-Next option in the single post page to navigate though the posts and this is what I'm using for the next button.
<?php echo get_permalink(get_adjacent_post(false,'',false)); ?>
But I can't figure out a way to link this same button to the very first posts when there are no more new posts to show. The reason is that the posts are used to show products.
Please note: To link Prev button in oldest post to the newest post I have used used this code.
<?php $next_page=get_permalink(get_adjacent_post(false,'',true));
$current_page=get_permalink();
if($next_page==$current_page){
$args = array( 'numberposts' => '1', 'category' => CAT_ID );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo get_permalink($recent["ID"]);
}
} else {
echo $next_page;
}
?>
add the args to query whatever posts are part of the products.
$posts_array = get_posts( $args );
get_permalink($posts_array[0]->ID); // First posts;
$args should be something like this (make sure it returns all the products posts):
$args = array(
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'ASC',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);