Update all posts meta at the same time in Wordpress - php

I want to run a function that will update all posts. My problem is that the function only runs when I visit that specific post (Only the specific post will be updated).
My function updates Facebook likes from the post source URL
function update_facebook_likes($content) {
global $post;
$url_path = get_post_meta(get_the_ID(), 'url_source', TRUE);
$data = file_get_contents('https://graph.facebook.com/v2.2/?id='.$url_path.'&fields=share&access_token=****');
$obj = json_decode($data);
$like_na = $obj->{'share'};
$like_no = $like_na->{'share_count'};
update_post_meta($post->ID, 'fb_likes_count', $like_no);
}
add_action('wp', 'update_facebook_likes');
function display_fb_likes() {
global $post;
$fb_likes_count = get_post_meta($post->ID, 'fb_likes_count', true);
echo $fb_likes_count;
}

Try to use this foreach loop to update all posts. using get_posts() function
https://developer.wordpress.org/reference/functions/get_posts/
$args = array(
'posts_per_page' => -1,
);
$posts_array = get_posts( $args );
foreach($posts_array as $post_array)
{
update_post_meta($post_array->ID, 'fb_likes_count', true);
}

Related

WORDPRESS: wp_set_post_terms not adding tags to CPT

I build a form that allow users to register sells using the frontend. I'm trying to make it pass some taxonomies that will be used to create a filter later, but even with wp_set_post_terms function the taxonomy is not being checked/tick'd at the backend when the user add/select it at the frontend.
Here is the code snippet:
<?php
global $wpdb;
global $wp_taxonomies;
$user_id = get_current_user_id();
if(isset($_POST['submitted'])) {
if($_POST['submitted'] == 'f16fca78e22e3f296692e5bbe0b5f7f0597a0bf9') {
$codigo_reserva = $_POST['codigo-reserva'];
$pax_principal = $_POST['pax-principal'];
$tag_acomodacao = $_POST['tag-acomodacao'];
$quantidade_pax = $_POST['quantidade-pax'];
$check_in = $_POST['check-in'];
$check_out = $_POST['check-out'];
$valor_venda = $_POST['valor-venda'];
$args = array(
'post_type' => 'vendas',
'post_title' => $codigo_reserva,
'post_status' => 'private',
'post_author' => $user_id,
);
$post_id = wp_insert_post($args);
$post_tags = array($tag_acomodacao);
wp_set_post_terms($post_id, $post_tags, 'acomodacao_reserva', true);
update_field('venda_pax_principal', $pax_principal, $post_id);
update_field('venda_pax_qtd', $quantidade_pax, $post_id);
update_field('checkin_venda', $check_in, $post_id);
update_field('checkout_venda', $check_out, $post_id);
update_field('venda_valor', $valor_venda, $post_id);
}
}
Everything works well except the taxonomy
Already tried to initialize $wp_taxonomies, use the explode function at the second parameter of wp_set_post_terms, pass the array as slug, and pass the array as ID

change the final number of the permalink by +1 php

these days I wonder how I can change the final permalink number of my posts by +1, i found this code on the net, but it needs to be changed someone help me?
// Get all posts
$query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
));
$posts = $query->get_posts();
foreach ($posts as $post) {
// Get permalink
$url = $post->post_name;
// old url
$old_url = array(
'http://localhost:8888/site/testpermalink-1/',
'http://localhost:8888/site/testpermalink-1/',
'http://localhost:8888/site/testpermalink-7/',
'http://localhost:8888/site/testpermalink-4/',
'http://localhost:8888/site/testpermalink-36/',
);
// Replacement
//$replacement = '';
// Replace url
//$new_url = str_replace($old_url, $replacement, $url);
// Prepare arguments
$args = array(
'ID' => $post->ID,
'post_name' => $new_url,
);
// Update post
wp_update_post( $args );
}
}
I would like this list to be changed with the final number +1, so this list after editing should be:
$new_url = array(
'http://localhost:8888/site/testpermalink-2/',
'http://localhost:8888/site/testpermalink-2/',
'http://localhost:8888/site/testpermalink-8/',
'http://localhost:8888/site/testpermalink-5/',
'http://localhost:8888/site/testpermalink-37/',
);
however then these permalink should be automatically saved after you change them.
Permalink is the post_name in database, sometimes called "slug".
You can do something similar:
foreach ($posts as $post) {
// split it into pieces, dividing by - character
$pieces = explode('-', $post->post_name);
//replace last piece adding 1
$pieces[array_key_last($pieces)] = (int)$pieces[array_key_last($pieces)] + 1;
//restore pieces
$post->post_name = implode('-', $pieces);
wp_update_post( $post );
}
WARNING: array_key_last function exists from PHP 7.0!

Get array of items from phpAdmin Wordpress

In my phpAdmin I have a list of arrays that I need to be able to access in php Wordpress.
I need to get these two arrays and the variables associated with them but I can't find anywhere how to access this type of information.
Essentially I would like to loop through all these items and match one of their variables with the ID of specific posts - I have the post part.
wp_learnpress_sections
wp_learnpress_section_items
Basically the item_type is an lp_lesson which is a custom post type. I am able to grab all the posts from wp_posts so I figured I would be able to grab these other arrays?
Edit:
My theme function. This works for all the posts. However, I want to be able to find out which section_id a post belongs to.
add_action( 'wp_enqueue_scripts', 'win_9388244_format_lp_lesson' );
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
foreach($posts as $post) {
update_post_meta( $post->ID, 'wpk_icon_text', 'Test' );
}
}
Full function that does it:
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
global $wpdb;
$sections = $wpdb->get_results( "SELECT section_item_id, section_id, item_id FROM wp_learnpress_section_items", ARRAY_A );
$items = $wpdb->get_results( "SELECT section_course_id, section_id FROM wp_learnpress_sections", ARRAY_A );
foreach($posts as $post) {
$lesson_id = $post->ID;
foreach($sections as $section) {
if($section['item_id'] == $lesson_id) {
$currentSection = $section['section_id'];
foreach($items as $item) {
if ($item['section_id'] == $currentSection) {
$course = $item['section_course_id'];
//switch $course with predefined variables for courses
}
}
}
}
}
}

Unable to use the get_excerpt function in wordpress

I am new to wordpress and php. I am trying to get the excerpt of a post retrieved using foreach in wordpress. I am trying to display all the posts on a page but I dont want to display the whole content I want only excerpt.
I am able to displat the title but unable to get the excerpt.
I have tried: $excerpt = get_post_excerpt($post),$excerpt = get_the_excerpt($post);, $excerpt = $post->the_excerpt()
please also tell me if iam missing some basics.
here is my full code
<?php
function some_code() {
// query
$query = 'orderby=date&order=asc&posts_per_page=-1';
$wpq = new WP_Query($query);
$posts = $wpq->get_posts();
foreach($posts as $post)
{
$link = get_permalink($post);
echo "<a href='$link'><h3>{$post->post_title}</h3></a>";
$excerpt = get_the_excerpt($post);
echo "$excerpt";
}
}
?>
An easy solution compatible with your code would be to use setup_postdata($post); inside your foreach loop, which makes all the post related data available:
$query = 'orderby=date&order=asc&posts_per_page=-1';
$wpq = new WP_Query($query);
$posts = $wpq->get_posts();
foreach($posts as $post)
{
setup_postdata($post);
$excerpt = get_the_excerpt($post);
echo $excerpt;
}
More about setup_postdata() can be found here.
Try this also if that doesn't work, I think using a traditional loop instead of foreach is a better approach:
$query = array('orderby' => 'date', 'order' => 'asc', 'posts_per_page' => '-1');
$wpq = new WP_Query($query);
if($wpq->have_posts()){
while($wpq->have_posts()){
$wpq->the_post();
the_excerpt();
}
}
Or you can use a function called wp_trim_words:
echo wp_trim_words( $post->post_content );

Can't get parmalink from Wordpress database

i want to fetch all post and insert into custom table but i can't get url (Parmalink) of post but it's always null or get error.
$posts = get_posts(array( "showposts" => 50));
global $wpdb;
foreach($posts as $post)
{
$wpdb->insert('wp_employee', array('pottitle'=>$post->post_title, 'postid'=>$post->ID, , 'postid'=>$post->permalink), array('%s', '%s', '%s'));
}
Try this and let me know if any issue ;)
function getp($s)
{
$result = post_permalink( $s );
return $result;
}
Call the function
$posts = get_posts(array( "showposts" => 50));
global $wpdb;
foreach($posts as $post)
{
$wpdb->insert('wp_employee', array('pottitle'=>$post->post_title, 'postid'=>$post->ID, 'postid'=>$post->getp($post->ID)), array('%s', '%s', '%s'));
}
Also you can call direct with using post_permalink
'lastname'=>post_permalink($post->ID))
get_posts() does not return value like permalink
try to check http://codex.wordpress.org/Template_Tags/get_posts
so you need to make manually permalink with postid or use get_permalink() or the_permalink()
You can use the following line inside the foreach loop
$permalink = get_permalink($post->ID);
and use $permalink wherever you want in foreach loop

Categories