Even viewing many answers here at StackOverflow did not solve the issue. I am using a page template and when I open that page the query runs but creates many duplicates. I am entering the 6000 simple records once it loads. I am also not logged in to the browser where I run this query. i have data in row[0] , row[1] ,row[2] .
Note: i am checking the post title with post_exists before inserting it but its still inserting the duplicates the original post url likes .com/my-post/ and duplicated record with , .com/my-post-2/
here is my code,
$postarr = array(
'post_status' => 'publish',
'post_title' => $row[0],
'post_content' => $row[0],
'post_author' => 1,
'post_type' => 'post' ,
'tags_input' => $cat_tags
);
$row_zero = htmlspecialchars_decode($row[0]);
if(!post_exists($row_zero)) {
$post_id = wp_insert_post( $postarr , $wp_error = false, $fire_after_hooks = false );
wp_set_object_terms($post_id, $row[2], 'writer');
}
Related
Is there a way to generate multiple new pages in wordpress with the same template, content and everything else except page name in Wordpress. The names of the new pages will be from a list of about 300 cities.
I found a plugin called BulkPress where i can insert the list and it generates the pages but with the default template and no content.
EDIT: Sorry i missed one important thing!
All the pages need to be subpages of one already made parent page and the template and content needs to be copied from that parent!
You could create this with programmatically. Put below code in your active template functions.php. It will insert 300 pages.
wp_insert_post( array $postarr, bool $wp_error = false, bool $fire_after_hooks = true )
e.g.,
foreach($i=1;$i<=300;$i++){
$my_post = array(
'post_title' => wp_strip_all_tags( "Your post Title" ),
'post_type' => 'page',
'post_content' => "Post Content Goes here",
'post_status' => 'publish',
'post_author' => 1,
'page_template' => 'template-blog.php',
'post_author' => get_user_by( 'id', 1 )->user_id,
);
// Insert the post into the database
wp_insert_post( $my_post );
I am using Gravity form of Wordpress and showed its entry on Frontend
with R & D I found the query which show the entries but i am not able to add pagination and sort order in this
$entries = GFAPI::get_entries(
$form_id,
array(),
null,
array('offset' => 0, 'page_size' => 99999)
);
Many thanks in advance for help :)
You need to familiarize yourself to the structure of the function GFAPI::get_entries. Try the following keeping in mind that the sorting key (5) is just an example and you should use your own sorting key:
$search_criteria = array(); // or $search_criteria['status'] = 'active';
$sorting = array( 'key' => '5', 'direction' => 'ASC' ); //5 is just an example
$paging = array( 'offset' => 0, 'page_size' => 25 );
$entries = GFAPI::get_entries( $form_id, $search_criteria, $sorting, $paging );
I'm using WordPress meta data to register clicks on images, to know which images each user has clicked - and also the total number of clicked images per user. The first part is fine, but I'm struggling to get the counter going, as it's returning a lower amount of meta data than what is actually there.
I have a custom post type gallerier and each gallery has a number of images. I'm using the meta key nedlasting, and I'm identifying each image individually by fetching the url.
Here is how I register clicks, after checking it isn't already:
// Add meta query if it doesnt already exist
function sjekk_nedlasting( $postid, $url, $dato) {
$brukerid = (string)get_current_user_id();
// Check if the image is downloaded previously
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $url),
'compare' => 'LIKE'
),
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// Perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
// If not already downloaded, register it
if ( empty( $nedl_ids ) ) {
$metaarray = Array(
'user_id' => $brukerid,
'url' => $url,
'date' => $dato
);
add_post_meta( $postid, 'nedlasting', $metaarray );
}
}
Then I'm trying to count those registered clicks using the following function:
// Count number of downloads for a single user
function tell_nedlastinger() {
$brukerid = (string)get_current_user_id();
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
return count($nedl_ids);
}
The function returns a number, but always much lower than the actual amount of registered meta data/clicks. Anyone seeing a problem?
Edit: I'm pretty sure the problem is that I'm getting the total number of posts, not the total number of meta data entries/clicks - which more often that not is several per post. Any way around that?
I never found a solution to query meta data and get x results per post, so instead of using an array in a single key I split the post meta data into three keys. Then I used $wpdb for a custom query. The final code for the tell_nedlastinger() function:
$brukerid = (string)get_current_user_id();
global $wpdb;
$query = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')");
return count($query);
You can improve the speed of this by using:
$brukerid = (string)get_current_user_id();
global $wpdb;
$count = $wpdb->get_row("SELECT COUNT(*) AS THE_COUNT FROM $wpdb->postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')");
return $count->THE_COUNT;
The speed is improved by only having to fetch one row from the database. Also note the use of:
FROM $wpdb->postmeta
This allows for a variable table prefix instead of assuming a table wp_ prefix.
$metaCount = count( get_comment_meta($postId,'key','value') );
I am using Wordpress and for the posts on my homepage, I want them to get ordered both randomly and by name. What I mean is, I want to show different posts on my home page each time, yet ordered by their names. I changed the WP_Query args from my theme files as below, but it didn't work. I'm getting unrelated results for a reason I can't understand.
#setup wp_query
$args = array(
'posts_per_page' => $postsperpage,
'orderby' => array( 'rand', 'title' ),
'order' => 'DESC',
);
Is there any way to make that possible?
p.s. I am honestly sick of those who downvote the questions irrelevantly. I wouldn't have asked the question should i had found a solution on the internet. If you have a logical reason please either warn me or edit my post instead of blindly downvoting it.
As documented:
orderby (string | array) - Sort retrieved posts by parameter. Defaults to 'date (post_date)'. One or more options can be passed.
So:
$args = array(
'orderby' => array( 'rand', 'name' ),
'order' => 'DESC',
);
But I don't think this will get you the result you desire. You will most probably have to only use rand with posts_per_page setting to get your desired number of posts. Fetch all posts and sort these by name afterwards.
Example:
$args = array(
'orderby' => 'rand',
);
// get X random posts (10 by default)
$result = new WP_query( $args );
// sort these posts by post_title
usort( $result->posts, function($a, $b) {
if ( $a->post_title == $b->post_title )
return 0;
return ($a->post_title < $b->post_title) ? -1 : 1;
} );
// Start the loop with posts from the result.
while ( $result->have_posts() ) : $result->the_post();
// do your stuff in the loop
}
I tried the following:
$insert = $wpdb->insert( $prefix."posts", array("post_title" => $posTitle,"post_content" => $postContent,"post_status" => "publish","post_type" =>"product"));
Using this query I Insert post when I check my post from admin and click on view is does not work when I update post from backend then it will work but I want to be view post without update it from admin mean afetr insert post with query post should work also use :
add_post_meta( $select[0]->productsId, '_wti_like_count', 0, true );
add_post_meta( $select[0]->productsId, '_wti_unlike_count', 0, true );
add_post_meta( $select[0]->productsId, '_wti_total_count', 0, true );
without any success. Can someone help solve this problem?
You shouldn't be using an insert for this purpose.
There's a function for exactly what you're trying to do.
wp_insert_post() - http://codex.wordpress.org/Function_Reference/wp_insert_post
$post_id = wp_insert_post( array(
'post_title' => $posTitle,
'post_content' => $postContent,
'post_type' => 'product',
'post_status' => 'publish',
) );
The post title variable is misspelled intentionally to match the example provided.