I saw in here that I can get a post's content in WordPress using the post ID. Something like:
<?php $my_postid = 83;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;?>
I want the very same thing, BUT getting the post by its name.
You can do it using
$content_post = get_posts( array( 'name' => 'yourpostname' ) ); // i.e. hello-world
if( count($content_post) )
{
$content = $content_post[0]->post_content;
// do whatever you want
echo $content;
}
Update : Also you can add this function in your functions.php and can call it from anywhere
function get_post_by_name($post_name, $post_type = 'post', $output = OBJECT) {
global $wpdb;
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $post_name, $post_type ));
if ( $post ) return get_post($post, $output);
return null;
}
// call the function "get_post_by_name"
$content_post = get_post_by_name('hello-world');
if($content_post)
{
$content = $content_post->post_content;
// do whatever you want
echo $content;
}
Update : To get a post by it's title you can use
// 'Hello World!' is post title here
$content_post = get_page_by_title( 'Hello World!', OBJECT, 'post' );
or you can use your $item->item_title variable
$content_post = get_page_by_title( $item->item_title, OBJECT, 'post' );
if($content_post)
{
$content = $content_post->post_content;
// do whatever you want
echo $content;
}
Related
my problem is that: Trying to retrieve the_content with a simple shortcode function, it retrieves only the title.
Even applying another filters the result is always the same.
The content is from a page.
The function is declared in the functions.php theme file.
Using the post (page) id.
function shtcode_Func( $atts = array() ) {
// set up default parameters
extract(shortcode_atts(array(
'id' => '5'
), $atts));
$my_postid = $atts;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
add_shortcode('shortcodePage', 'shtcode_Func');
Calling from widget with [shortcodePage id=POST_ID] (int)
Result: Prints only the title.
I tried to change the filter with 'the_post_thumbnail' and retrieved the title again.
I'm desperated :(
Thanks!!
There are several things incorrect with your shortcode function, but the main things:
You are using extract but not using anything from extract
$atts is an array, not just the id.
You are using apply_filters('the_content'). This essentially overwrites WPs built in apply_filter. You want to use add_filter, but as you can see that won't be necessary.
Here is the shortcode trimmed down with what you are trying to do:
function shtcode_Func( $atts ) {
// set up default parameters. No need to use extract here.
$a = shortcode_atts(array(
'id' => ''
), $atts);
// Use get_the_content, and pass the actual ID
$content = get_the_content('','', $a['id'] );
// This is the same
$content = str_replace(']]>', ']]>', $content);
// Return the content.
return $content;
}
add_shortcode('shortcodePage', 'shtcode_Func');
Try to use like this:
function shtcode_Func( $atts = array() ) {
// set up default parameters
extract(shortcode_atts(array(
'id' => '5'
), $atts));
$content_post = get_post( $atts['id'] );
ob_start();
$content = $content_post->post_content;
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
echo $content;
$str = ob_get_contents();
ob_end_clean();
return $str;
}
add_shortcode('shortcodePage', 'shtcode_Func');
I'm trying to fetch specific post content by ID for a custom post type.
I've tried the following solutions and a few more. At best I seem to be retrieving the title. But none of the content.
1:
<?php
$post_id = 15002;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $queried_post->post_content;
?>
2:
<?php
$my_postid = 15002;
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
3:
<?php
$ID = 15002;
$args = array('p' => $ID, 'post_type' => 'ct_template');
$loop = new WP_Query($args);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php global $post; ?>
<?php the_content () ?>
<?php endwhile; ?>
If you are using Toolset and want to get content by page Id then use below code in your function.php
add_filter( 'wpv_filter_content_template_output', 'get_content_template_id', 99, 4 );
function get_content_template_id( $content, $template_selected, $id, $kind ) {
global $current_archive_template_id;
$current_archive_template_id = $template_selected; // $template_selected = current Content Template ID
return $content;
}
Then use below code to get title and content
$content_template_title = get_the_title($current_archive_template_id);
$content_template_content = get_the_content($current_archive_template_id);
In WordPress I need to fetch name of author who created post using author_id.
How can I find author_name ?
You can use get_the_author_meta(), to get author data.
echo get_the_author_meta('display_name', $author_id);
Hope this helps!
This should work like charm
<?php echo get_the_author(); ?>
For more detailed information.
https://codex.wordpress.org/Function_Reference/get_the_author
Use below code in single.php or the relevant page you want author name
<?php get_the_author_meta( 'display_name', $author_id ); ?>
When used in the WordPress Custom REST API endpoint you can do it like this:
function customrestapiplugin_getpost( $slug ) {
$args = [
'name' => $slug['slug'],
'post_type' => 'post'
];
$post = get_posts($args);
$data[$i]['id'] = $post[0]->ID;
$data['title'] = $post[0]->post_title;
$data['content'] = $post[0]->post_content;
$data['excerpt'] = $post[0]->post_excerpt;
$data['slug'] = $post[0]->post_name;
$data['date'] = $post[0]->post_date;
$data['link'] = get_permalink($post[0]->ID);
$data['author'] = get_the_author_meta('display_name', $post[0]->post_author);
$data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
$data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
$data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');
return $data;
}
Add This code in single-post.php
<?php echo get_the_author(); ?>
I hope this will work !!
For those who are looking for a seamless solution on how to get an author of a post in WordPress, this is another lightweight method.
global $wpdb;
$post_id = 12; // your post id
$post_author_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_author FROM {$wpdb->posts} WHERE ID = %d ", $post_id ) );
$author = new WP_User( $post_author_id );
$display_name = $author->display_name;
$avartar = get_avatar( $post_author_id, 30 ); // get_avatar( userid, size )
$author_url = get_author_posts_url( $post_author_id );
I need in the act of publishing a post, a value insert in a custom_field, the same post_title value!
function add_custom_field_automatically_two($post_ID) {
global $post;
$post_id = $post->ID;
global $wpdb;
//$my_customf = $post->post_title;
//$my_customf = get_the_title( $post_id );
$my_customf = "something string"; // worked, but I need post_title!
add_post_meta($post_ID, 'my_customf', $my_customf, true);
}
add_action('publish_page', 'add_custom_field_automatically_two');
add_action('publish_post', 'add_custom_field_automatically_two');
?>
Thanks.
I added a custom php function to my wordpress template, where I want to echo the content of pages under a certain parent:
Departments
-department 1 (get the title and the content clean)
-department 2 (get the title and the content clean)
(...)
The code I have so far is not working as i want, the title is fine but I need to filter the content so I can grab only the text between the "<p>" tag. Is this possible? Thank you.
functions.php
function echo_childs_of( $postID ) {
$args = array(
'order' => 'ASC',
'post_parent' => $postID,
'post_status' => null,
'post_type' => 'any'
);
$page_childs = get_children( $args );
if ( $page_childs ) {
foreach ( $page_childs as $child ) {
$title = get_the_title( $child );
$content = get_the_content($child);
$content = strip_shortcodes( $content );
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content);
echo $title;
echo $content;
}
}
}
on my php page
echo_childs_of( 7 );
You could use DOM to find the p tag elements and itarate over them using a for cycle to do whatever it is that you want to do.
$content = get_the_content($child);
$doc = new DOMDocument();
#$doc->loadHTML($content);
$p_elements = $doc->getElementsByTagName('p');
foreach ($p_elements as $p) {
//Do something with the p element... Strip tags maybe?
}
Try this:
$content = get_the_content($child);
preg_match('/<p>(.*)<\/p>/', $content, $match);
$content = $match[1];