It sure can be written in a more efficient way (untested):
$added = array();
global $wpdb;
foreach($uniques as $unique){
$pagetitle = getTitle($unique);
$new_post = array(
'post_title' => $unique,
'post_status' => 'publish',
'post_type' => 'websites'
);
$pid = wp_insert_post($new_post);
if ($pid) {
$wpdb->query( $wpdb->prepare(
"INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES
(%d, '%s', '%s'), (%d, '%s', '%s')",
$pid, 'title', $pagetitle, $pid, 'url', $unique
) );
$added[] = $pid;
}
}
if ( ! empty($added) ) {
$ids = implode(',', $added);
$wpdb->query("UPDATE $wpdb->posts SET `post_title` = `ID`, `post_name` = `ID` WHERE `ID` IN ($ids)");
}
With 600 urls this code runs 1200 queries less than your, but maybe is not enough…
Related Posts:
- save_post + insert_post = infinite loop
- Add Incrementing ID to each paragraph in the_content
- post__in – Placing content from a foreach loop inside of an array
- wp_insert_posts Fatal error: Maximum function nesting level of ‘100’ reached, aborting!
- In a foreach loop, how do I target the last item in the loop?
- Automatically insert php function into post $the_content
- How to iterate through database until it find a match
- Get all posts including sticky ones with get_posts(),setup_postdata(), and foreach loop?
- How to print out menu names in the foreach loop?
- Add class to last 3 posts in loop
- Output meta into arrays
- Need to add class to first element in a foreach loop
- Foreach displays from last to first
- Simple foreach loop help needed in wordpress “the loop”
- How to print redux gallery feilds id? [closed]
- How to check all items in a loop have post thumbnail?
- Get featured image outside the loop using foreach
- Why loop renders only last metabox from array? [closed]
- Foundation modal in wordpress php loop
- ACF field not appearing correctly in loop
- Eliminate duplicates in a foreach loop [closed]
- Wrap every 2 divs in row – for each loop [closed]
- ACF loops false value with repeater and checkbox
- show random image in loop if image is not set
- How do I style the first two posts of a loop
- Get Page content to displayin Bootstrap Modal
- WordPress Rewriting the DOM?
- How can I paginate this?
- Obtain page data and use regular loop
- How to display posts by vote count and if no value continue with latest posts with no votes?
- How can I load posts starting from, and including, the current post?
- Load wordpress content in other domain
- Genesis multiple category custom loop 404s after first page
- Custom Loop Using Shortcode in Custom Page Template
- Do I need to use The Loop on pages?
- Sort results by groups of numbers and letters
- Listing subpages title and content and styling the first iteration differently
- Post author for revision not being set on update/insert
- Infinite Scroll on a loop?
- Shortcode in posts called by ajax is not working
- Use of wp_insert_post and parameters
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- How to fix pagination for custom loops?
- Creating a post from data returned from HTML form
- Getting error when using wp_insert_post()
- Modify the main loop to display current month / year
- Show all posts using the template page and the loop?
- Checkbox doesn’t stay checked, conten
- Showing sticky posts with get_posts()
- Pagination – works on local but not live dev!
- On single.php, fetch 2 posts created after and 2 posts created before in relation to the actual post (using menu_order)
- query_posts() vs get_posts() multiple loops [duplicate]
- Page-loop is looping content from custom loops on the same site. How to avoid that?
- need help looping add_action in wp
- Problem getting current post tags to show in a widget
- Modified loop-single-forum.php to work inside of a sidebar Widget, not working on some site pages
- List authors with the last post title and order by last post date
- How to get the role id from the role name?
- Does WordPress Loop have a function like Shopify’s cycle?
- How do I paginate a get_posts() request inside another page loop (Paginating childpages)
- Basic multiple loops only displaying first loop
- Using Javascript for Looped Content
- Latest Post Styled Different Than other Posts
- Display post list with different styles
- ‘Auto Draft’ Post showing in wordpress loop
- How do I display an image before the first post of the loop when I’m using get_template_part?
- ACF custom field in [closed]
- Split wordpress loop to multiple layouts
- Post classes in the loop are being changed in certain themes
- single.php – how to mark current page in the loop
- Problem with displaying HTML content after in_array [closed]
- post thumbnail, conditions, else wont work
- How do I make a variable available inside partials?
- How to hook into container
- Why does `inclusive` not work in this wordpress loop?
- Combine the results of two loops [closed]
- Widgets in the loop if active
- How do I automate multiple category loops?
- Added if statement to loop
- Both a page loop and posts loop on the same page
- WordPress: Loop Help
- How to get The Loop working with $wpdb->get_results()
- List posts that don’t have a specific tag?
- Changing the_posts_navigation() html output
- Making a query to the DB using same parameters of loop
- Woocommerce: How to display product price [closed]
- How to show more random posts if Tag has less than 3 posts
- Can we count the WordPress Loop
- Variable not being passed into WordPress loop
- Is wp_reset_postdata() redundant after the main loop?
- Cannot retieve the_content() and the_author() – both returning empty strings
- Exclude duplicated $sticky post from combined loop content
- Get post id within comments loop
- Exclude first post (sticky or not) from the loop using query_posts()
- How to order by multiple fields using standard query_posts?
- Multiple get_posts() queries on one page
- Loop for sticky posts
- Update status of all posts in a category
- Getting a custom field in the middle of a loop
- Syntax error when I try to insert my loop into an unordered list? [closed]