I'm importing from a JSON file and I get duplicates every time the importer runs.
The foreach
$name = $data->name;
$desc = $data->description;
$type = $data->type;
$tutor = $data->tutor;
$location = $data->location;
$planned_start = $data->planned_start;
$actual_start = $data->actual_start;
$actual_end = $data->actual_end;
$max_attendees = $data->max_attendees;
The importer
$check_title = get_page_by_title( $name);
$my_post = array(
'ID' => $check_title->ID,
'post_title' => wp_strip_all_tags($name),
'post_type' => 'training',
'post_content' => $desc,
'post_status' => 'publish',
'post_author' => 1,
'comment_status' => 'closed',
'ping_status' => 'closed',
'meta_input' => array(
'course_type' => $type,
'tutor' => $tutor,
'location' => $location,
'planned_start' => $planned_start,
'actual_start' => $actual_start,
'actual_end' => $actual_end,
'max_attendees' => $max_attendees,
),
'tax_input' => array(
'custom_cat_training' => $type,
),
);
if (empty($check_title)){
$post_id = wp_insert_post($my_post);
wp_set_object_terms($post_id, $type, 'custom_cat_training');
} else {
$post_id = wp_update_post($my_post);
wp_set_object_terms($check_title->ID, $type, 'custom_cat_training');
}
I'm probably missing something obvious but I can't for the life of me work it out.
you can check it with it
https://developer.wordpress.org/reference/functions/post_exists/
if (!post_exists($name)){
$post_id = wp_insert_post($my_post);
wp_set_object_terms($post_id, $type, 'custom_cat_training');
} else {
$post_id = wp_update_post($my_post);
wp_set_object_terms($check_title->ID, $type, 'custom_cat_training');
}
I used get_page_by_path($trimmed, OBJECT, 'training');
and used the page slug rather than title
Related
I am trying to make custom validation to cf7 form and then store data in a custom post type if everything is valid. My problem is that even when form inputs are not valid form still sends email and store its data. Can you help me with that. Below is my code. What should I use to validate inputs first and only then store data.
Store posted data:
function save_posted_data_ios( $posted_data ) {
if( isset($posted_data['menu-377']) == "IOS" ){
$args2 = array(
'post_type' => 'np_coupon_code',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'np_ccode__email',
'value' => 'null',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'np_ccode__type',
'value' => 'IOS'
)
)
);
$posts_query2 = new WP_Query($args2);
$the_count_ios = $posts_query2->found_posts;
$query = new WP_Query($args2);
while ( $query->have_posts() ) : $query->the_post();
$id = get_the_ID();
$code = get_the_title();
endwhile;
if( isset($posted_data['your-email']) ){
update_post_meta($id, 'np_ccode__email', $posted_data['your-email']);
}
$to = $posted_data['your-email'];
$subject = 'Subject';
$body = $code;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
return $posted_data;
wp_reset_query();
}
}
add_filter( 'wpcf7_posted_data', 'save_posted_data_ios' );```
Custom error:
function my_wpcf7_validate_ios( $result_ios, $tag ) {
$type = $tag['type'];
$name = $tag['menu-377'];
$args2 = array(
'post_type' => 'np_coupon_code',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'np_ccode__email',
'value' => 'null',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'np_ccode__type',
'value' => 'IOS'
)
)
);
$posts_query2 = new WP_Query($args2);
$the_count_ios = $posts_query2->found_posts;
if ( $the_count_ios == 0 && $_POST['menu-377'] == "IOS"){
$result_ios->invalidate( $tag, wpcf7_get_message( 'invalid_ios' ) );
}
return $result_ios;
}
add_filter( 'wpcf7_validate_select*', 'my_wpcf7_validate_ios' , 10, 2 );
add_filter( 'wpcf7_messages', 'mywpcf7_ios_messages' );
function mywpcf7_ios_messages( $messages ) {
return array_merge( $messages, array(
'invalid_ios' => array(
'description' => __( "No IOS Coupons Left", 'contact-form-7' ),
'default' => __( 'No IOS Coupons Left.', 'contact-form-7' )
)
));
}
I want to get a list of products with sku and post id with this code, everything seems fine and ok with this code :
$statuses = array('publish', 'draft');
// Args on the main query for WC_Product_Query
$args = [
'status' => $statuses,
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
];
$vendor_products = wc_get_products($args);
$list_array = array();
foreach ($vendor_products as $key => $product) {
if ($product->get_type() == "variable") {
// Args on product variations query for a variable product using a WP_Query
$args2 = array(
'post_parent' => $product->get_id(),
'post_type' => 'product_variation',
'orderby' => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ),
'fields' => 'ids',
'numberposts' => -1,
);
foreach ( get_posts( $args2 ) as $child_id ) {
// get an instance of the WC_Variation_product Object
$variation = wc_get_product( $child_id );
if ( ! $variation || ! $variation->exists() ) {
continue;
}
$list_array[] = array(
'sku' => $variation->get_sku(),
'postid' => $variation->get_id()
);
}
} else {
$list_array[] = array(
'sku' => $product->get_sku(),
'postid' => $product->get_id()
);
}
}
I have total 1660 (470 published and 1,190 drafted) products but it's just returns 501 products and i don't know why!
this is my products in woocommerce:
this is the final result of query :
this is the result of Janki's code
// Woocommerce get all products
$args = array(
'post_type' => array('product','product_variation'),
'meta_key' => '_sku',
'post_status' => array('publish','draft'),
'meta_query' => array(
array(
'key' => '_sku',
'compare' => 'EXISTS',
),
),
);
$products = new WP_Query($args);
/* using this code you can get all products */
/* this may help you to get details */
I am trying to insert a few fake posts into my WordPress search results. Using the pre_get_posts hook, I am able to get the function to trigger, but I am not able to prepend the fake posts to the WordPress results.
I followed another post about inserting fake posts. The article mention inserting the fake post into the wp-cache.
Any help would be greatly appreciated.
function extra_search_items($query) {
if ($query->is_search && !is_admin()) {
global $wp, $wp_query;
$FakePosts = array(
array(
'ID' => -199,
'post_title' => 'Fake 1',
'post_content' => 'This is a fake virtual post.',
'post_date' => '2018-06-22 00:00:00',
'comment_status' => 'closed',
'post_type' => 'post'
),
array(
'ID' => -200,
'post_title' => 'Fake 2',
'post_content' => 'This is a fake virtual post.',
'post_date' => '2018-06-22 00:00:00',
'comment_status' => 'closed',
'post_type' => 'post'
)
);
$i = 0;
$post = array();
foreach ($FakePosts as $blog) {
// create the post and fill up the fields
$post[$i] = new WP_Post((object)array(
'ID' => $blog['ID'],
'post_title' => $blog['post_title'],
'post_content' => $blog['post_content'],
'post_date' => $blog['post_date'],
'comment_status' => $blog['comment_status'],
'post_type' => $blog['post_type']
));
if(!wp_cache_get($post[$i]->ID, 'posts')) {
wp_cache_set($post[$i]->ID, $post[$i], 'posts');
array_unshift($wp_query->posts, $post[$i]);
$wp_query->post_count++;
}
$i++;
}
}
return $wp_query;
}
add_action('pre_get_posts','extra_search_items');
You created fakeposts array, but you are not combining the fakeposts with with the original posts.
Combine the two arrays and loop it then it will display fakeposts also,
like the following
function extra_search_items($query) {
if ($query->is_search && !is_admin()) {
global $wp, $wp_query;
$FakePosts1 = array(
array(
'ID' => -199,
'post_title' => 'Fake 1',
'post_content' => 'This is a fake virtual post.',
'post_date' => '2018-06-22 00:00:00',
'comment_status' => 'closed',
'post_type' => 'post'
),
array(
'ID' => -200,
'post_title' => 'Fake 2',
'post_content' => 'This is a fake virtual post.',
'post_date' => '2018-06-22 00:00:00',
'comment_status' => 'closed',
'post_type' => 'post'
)
);
$i = 0;
$post = array();
$FakePosts_array = array_merge($FakePosts1,$FakePosts)
foreach ($FakePosts_array as $blog) {
// create the post and fill up the fields
$post[$i] = new WP_Post((object)array(
'ID' => $blog['ID'],
'post_title' => $blog['post_title'],
'post_content' => $blog['post_content'],
'post_date' => $blog['post_date'],
'comment_status' => $blog['comment_status'],
'post_type' => $blog['post_type']
));
if(!wp_cache_get($post[$i]->ID, 'posts')) {
wp_cache_set($post[$i]->ID, $post[$i], 'posts');
array_unshift($wp_query->posts, $post[$i]);
$wp_query->post_count++;
}
$i++;
}
}
return $wp_query;
}
add_action('pre_get_posts','extra_search_items');
In WordPress I have a loop with some $args array, which I want to be able to edit conditionally depending on a $_GET value.
$args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
'fields' => 'ids',
'no_found_rows' => true,
'post__in' => $post__in,
'nopaging' => true
);
When adding to $args array like this:
if ( $_GET['prop_sort'] == 'price' ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = $prefix . 'price';
}
Nothing changes, and added to $args stuff gets ignored. However when I put code like this:
args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
'fields' => 'ids',
'no_found_rows' => true,
'post__in' => $post__in,
'nopaging' => true
);
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = $prefix . 'price';
Everything works as intended and posts are sorted by price from small to big.
What could be the problem?
EDIT:
Trying to influence query like this:
function myplugin_pre_get_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'property' ) {
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
$query->set( 'meta_key', 'sb_price' );
}
return $query;
}
add_action( 'pre_get_posts', 'myplugin_pre_get_posts', 1 );
Dones't work either (Undefined index: post_type in ..\functions.php on line 65).
here is my wordpress new query loop $args
if (isset($_GET['list_shows'])) {
if ($_GET['list_shows'] == 'update') {
$orderby = 'modified';
$order = '';
} elseif ($_GET['list_shows'] == 'views') {
$orderby = 'meta_value_num';
$order = 'DESC';
} elseif ($_GET['list_shows'] == 'popularity') {
$orderby = 'comment_count';
$order = 'DESC';
}
} else {
$orderby = 'modified';
$order = '';
}
$argz=array(
'posts_per_page' => '-1',
'orderby'=>$orderby,
'order'=>$order,
'meta_query' => array(
array(
'key' => 'fragman',
'compare' => 'NOT EXISTS',
'posts_per_page' => '-1',
)
),
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => '2 days ago',
),
'posts_per_page' => '-1',
),
);
the question is about ordering posts by meta_value_num , descending ?
my post views meta key = views, could we add this ordering parameter in this meta query ?
if it is, how can we do it. ?
Thanks.
You can pass like this
global $wp_query;
$args = array(
'meta_key' => 'views',
'orderby' => 'meta_value meta_value_num',
'order' => 'DESC'
);
query_posts( array_merge( $args , $wp_query->query ) );
I hope this will work for you.