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
- How to place comments_template(); outside the loop?
- Insert/sticky specific post into Loop at specific location
- Style every four posts differently [duplicate]
- Return vs Echo Shortcode
- have_posts() return false but count says “3”
- next_posts_link not working in custom loop
- How to sort posts in archive loop
- Show top level comments ordered by number of replies
- Redirect Loop to Page, Can’t Find Origin, Can’t Remove [closed]
- wordpress change the loop order by dynamic value
- How to speed up a wordpress function with multiple loops?
- Post Format single-loop.php using get_template_part
- Use object in template part
- A loop with all articles with certain text strings in tags
- Prevent Headers from being sent
- How do I get row padding in a post feed?
- How can I create a legend (Key) like the one in the footer on WordPress StackExchange
- It is possible to encounter horrible bugs if I place a main loop that uses `query_posts` right above the main bbPress loop?
- Iterate through posts based on array of categories
- How to show “teaser” posts on blog / archive pages, otherwise full posts
- Make blog page show 10 on first page, 9 on every other page
- Alter query using pre_get_posts() if meta_key is present
- Next Posts Prev Posts for Standard post format only
- Alert Bar section within WP loop is displaying even though there are no posts
- Two loops on different pages do the same thing, but only one page works
- Posts active and expired in different loops
- Load wordpress content in other domain
- Genesis multiple category custom loop 404s after first page
- Shortcode in posts called by ajax is not working
- Using Javascript for Looped Content
- Making a query to the DB using same parameters of loop
- Variable not being passed into WordPress loop
- Exclude duplicated $sticky post from combined loop content
- Getting a custom field in the middle of a loop
- WP Loop. If featured image is a panorama (3:1 ratio) execute some code
- How to avoid duplicates when creating recent network posts
- Trying to get property of non-object in shortocde
- variable value disappears in a second loop
- How to Loop with the final result formatted differently?
- Tags on page (not post) returns nothing – why? Improved clarified question!
- Include future posts in tags and in search
- Display child page content of a specific parent on home page
- How to wrap every 3 posts in a div (and close the last div too) [closed]
- Dividing the loop to style post differently
- Show terms in archive page
- Problem in replacing the_content with the_excerpt()
- Loops for cat links not looping for wordpress site
- Sort posts according to a numeric value entered with ACF
- wp_get_attachment_url not fetching URL?
- get the value for ALT is we check to see if an actual ALT value is set, if not we use the caption and if no caption is set we use the title
- Display all categories including sub categories under a list ul
- How to get all multi-select user meta values and add them to an array?
- How to resolve a reload loop issue in the frontend when logged in to WordPress multi-site backend admin area
- Exclude Posts Using Meta Query and User Meta
- Excerpt isn’t working or am I missing something?
- Can I temporarily disable global $post?
- Shortcode for pulling specific Post Title outside loop when ID is passed in
- Show a list of user posts in the user admin page
- Efficient way of querying for a “fallback” post?
- Infinite Loop after Genesis Theme Upgrade
- Create a hierarchical loop at predefined markup requirements
- enumerating custom taxonomies?
- WordPress loop only displays 5 articles max?
- How to display thumbnail if post is assigned one otherwise not
- get_template_part() to render single page Theme
- Showing related posts (custom post type) outside of the loop
- Looping taxonomy in taxonomy?
- Post content not showing some content
- Paged homepage – requirements?
- Least Number of Loops to Create Custom Homepage?
- The Loop for my theme with static front page is not working
- WordPress loop: Display if posts exist
- How do I filter a custom post type loop by a field?
- Showing ads after posts in home page
- Create and publish a new post every Monday, listing the post titles and links to the posts published during the previous week
- Prevent duplicating specific column from database table
- wp_mail function not working in user query loop