Can't get parmalink from Wordpress database - php

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

Related

Foreach loop not looping through

I have a foreach loop which is only returning the title of the latest post. For example, I have the post test as the latest post in products and in the loop defined below, when doing var_dump, it only dumps the title for the latest post called "test".
Why is this?
Approach:
<?php
$args = array(
'post_type' => 'products',
'post_parent' => 0,
'posts_per_page' => 15,
);
$products = get_posts( $args );
if ($products){
foreach ($products as $product) : setup_postdata( $product );
var_dump(get_the_title());
endforeach;
wp_reset_postdata();
}
?>
foreach ($products as &$product) : setup_postdata( $product );
Please try this in your foreach loop.
Use this one
if ($products){
foreach ($products as $product) : setup_postdata( $product );
echo get_the_title($product->ID));
// or echo $product->post_title;
endforeach;
wp_reset_postdata();
}
It's strange but when you're wanting to use template tags along with setup_postdata() you need to use the global $post variable. setup_postdata() doesn't actually set that variable it sets some related global variables and runs the the_post action. You can see what happens here
To do what you want to do with out passing ids etc for every template function call you would need to setup your loop like this.
global $post;
foreach ( $products as $post ) {
setup_postdata( $post );
// Your code here.
}
wp_reset_postdata();// Reset the global $post variable and re-setup postdata.

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
}
}
}
}
}
}

Update all posts meta at the same time in Wordpress

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);
}

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 an echo to do what I want in Wordpress plugin

So I'm trying to write a new plugin since I haven't been able to find one that does exactly what I want with the extensibility that I desire. The goal of the plugin is to be able to use a simple shortcode to display an image slider that automatically populates with your blog's latest posts.
I've got the basic plugin files ready and the shortcode implemented and tested. I had a snafu solved yesterday on SO but the solution highlighted a new problem. Here's the code:
function slyd( $category, $slydcount ) {
global $post;
$tmp_post = $post; // Create $tmp_post to empty $post once Slyd is done with it
$args = array(
'category' => $category,
'numberposts' => $slydcount
);
$slydposts = get_posts( $args );
foreach( $slydposts as $post ) : setup_postdata($post);
$post_title = get_the_title(); // Get the post's title
$post_content = get_the_content(); // Get the post's content - will write code to get excerpt later
$post_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' ); // Get the post's featured image's src
$post_permalink = get_permalink(); // Get the post's permalink
echo '<h2>' . $post_title . '</h2>'
. '<p>' . $post_content . '</p>'
. '<p>' . $post_thumb . '</p>';
endforeach;
$post = $tmp_post; // Empty $post once Slyd is done with it
}
// Create the shortcode
function slyd_shortcode( $atts ) {
// $atts ::= array of attributes
// examples: [slyd]
// [slyd category='slide']
// [slyd slydcount='5']
// [slyd theme='default']
/* Retrieve attributes set by the shortcode and set defaults for
unregistered attributes. */
extract( shortcode_atts( array(
'category' => 'slyd', // Which category(s) to display posts from
'slydcount' => '5', // How many Slyds to display
'theme' => 'default' // Which Slyd theme to use
), $atts ) );
return "<p>category = {$category}, count = {$slydcount}</p>"
. slyd( $category, $slydcount );
}
add_shortcode( 'slyd', 'slyd_shortcode' );
The issue is in the foreach loop in function slyd();. I was originally using a return where the echo is now to put the result on the screen. That worked to display the first post but it would, of course, escape the function. I need it to cycle through and display all of the posts.
From researching the PHP documentation I found that I could use print in place of echo or return but it's giving me the same result as echo. What's happening is the code seems to be executing twice. It places itself where it needs to be the first time, then it also echoes to the browser and places it just below the head of the page.
I suppose my question is whether there's an alternative to return, echo, or print that would solve my problem?
Thanks in advance.
Now I'm trying to get the plugin to pull in the blog's latest posts but I'm running into a bit of a snafu. When I use the_title() and the_permalink() they display outside of the code I'm trying to contain them in. Further, the_content() is displaying once with the_permalink() and the_title() and then a second time where it's supposed to.
You can see the behavior here.
return is what you want in this case. You want to return a value (i.e. html code) from the slyd function so you can use it (in this case append it) in the slyd_shortcode function. However, you need to first collect all output into an additional variable ($ret below), and then return that value:
function slyd( $category, $slydcount ) {
global $post;
$tmp_post = $post;
$args = array(
'category' => $category,
'numberposts' => $slydcount
);
$slydposts = get_posts( $args );
$ret = '';
foreach( $slydposts as $post ) {
setup_postdata($post);
$post_title = get_the_title();
$post_content = get_the_content();
$post_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
$post_permalink = get_permalink();
$ret .= '<h2>' . $post_title . '</h2>'
. '<p>' . $post_content . '</p>'
. '<p>' . $post_thumb . '</p>';
}
$post = $tmp_post;
return $ret;
}
As you see you should first initialize the $ret variable with an empty string, then append to it on each iteration of the foreach loop. return is used to return the whole string after the loop.

Categories