Wordpress 3.0
I want to have the contents of a specific post into a page by using the title of the post. As far as I can tell, I can't do it directly with get_post().
I can assume what the brute force way might be, but I suspect there's a more elegant way?
get_page_by_title($id, OBJECT, 'post');
There ye go.
<!--1.Get post ID by post title if you know the title or the title variable-->
<?php
$posttitle = 'post_title';
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
echo $postid;
?>
<!--2.use get_post($post_id) to get whatever you want to echo-->
<?php
$getpost= get_post($postid);
$postcontent= $getpost->post_content;
echo $postcontent;
?>
No need to SQL query's when you can use wordpress own functions for this.
$page = get_page_by_title( 'Startsida' );
$page_id = $page->ID;
post_exists is a good function for that :
https://developer.wordpress.org/reference/functions/post_exists/
<?php
$post_id = post_exists('Your title');
// return id or 0 if post doesn't exists.
if($post_id>0)
get_post($post_id);
See my answer on a very similar question. Do not query the data base with an unescaped string.
You can use this:
1)
global $wpdb;
$your_title = "yourtitle";
$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = $your_title");
echo $id;
or 2)
$slug_to_get = 'my_title_or_slug';
// you can use custom post type too
$posttypee='post';
$args=array(
'title' => $slug_to_get,
'post_type' => $posttypee,
'post_status' => 'publish'
);
$my_posts = get_posts($args);
if( $my_posts ) {
echo 'ID on the first post found '.$my_posts[0]->ID;
}
Related
I'm developing wordpress plugin.
I need to find out post_id from thumbnail_id(not reverse !).
How can I do this?
You can get result by this code
global $wpdb;
$_thumbnail_id = {thumbnail id};
$sql = "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` = $_thumbnail_id";
$result = $wpdb->get_results( $sql, ARRAY_A );
//access first returned post id
var_dump($result[0]['post_id']);
If you added same image for multiple posts there will be multiple returns.
You can use get_the_ID() to get the post id. You can find this function in wp-includes/post-template.php
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
I need to get all users who commented on a post. And for each user the comments. Searched online but couldn't find any solution.
If post_id= 15 then
<?php
$comments = get_comments('post_id=15');
foreach($comments as $comment) :
echo($comment->comment_author .'--coment--'.$comment->comment_content);
echo '<br>';
endforeach;
?>
see this for further details https://codex.wordpress.org/Function_Reference/get_comments
.
You need get_comments().
Here is how you get all comments on a certain post:
<?php
$args = array(
'post_id' => 0, // Place post ID here.
);
get_comments( $args );
?>
After running this code, you will get an array of posts and user IDs, you can use the IDs with a foreach to get all comments for each user. Like this:
<?php
$args = array(
'user_id' => 0, // Place user ID here.
);
get_comments( $args );
?>
You can get comment writer from its comment id:
<?php $author = get_comment_author( $comment_ID ); ?>
You can generate short code use it, Here this will display all information of comment.You need to extract which one is required.
function input_func( $atts ) {
global $post;
$comments = get_comments( array( 'number' => 2, 'post_id' => get_the_ID() ) );
print_r( $comments);
}
add_shortcode( 'input', 'input_func' );
Code:
global $wpdb, $post;
// Query
$query = sprintf("SELECT comment_author_email
FROM {$wpdb->comments}
JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID
WHERE comment_post_ID = %d AND comment_approved = '1'",
$post->ID);
$emailslist = $wpdb->get_col($query);
$emailslist = array_unique($emailslist);
// get all emails
print_r($emailslist);
Here you can get list of emails of users.
You can also use WP_Comment_Query
I have a custom field, say, "mood", and I need to display list of tags for all posts that have "mood" = "grumpy". Is it possible to do this without fetching all posts and then fetching tags for each of them?
You can use the function get_posts();
$args = array(
'meta_key' => 'mood',
'meta_value' => 'grumpy',
);
$your_posts = get_posts( $args );
Untested, but this should achieve what you want. Just look out for typos and missed semi-colons!
While you don't get the whole post, you do still have to query the database for the ID of posts with 'mood' = 'grumpy', so unless you have lots of posts, it's probably easier to just go with the answer #Dorel gave.
$query = $wpdb->prepare('
SELECT ID
FROM %1$s
LEFT JOIN %2$s
ON %1$s.ID = %2$s.post_id
WHERE %2$s.meta_key = "mood"
AND %2$s.meta_value = "grumpy"
', $wpdb->posts, $wpdb->postmeta
);
$ids = $wpdb->get_col($query);
if(!empty($ids)) : foreach($ids as $post_id) :
$tags = wp_get_post_tags($post_id, $args);
if(!empty($ids)) : foreach($tags as $tag) :
$tags[] = $tag->name;
endforeach;
endif;
endforeach;
endif;
// Now you have an array of Tag names, output them as you wish
Codex for wp_get_post_tags = http://codex.wordpress.org/Function_Reference/wp_get_post_tags
Codex for wp_get_object_terms (to see what $args are available for wp_get_post_tags) =
http://codex.wordpress.org/Function_Reference/wp_get_object_terms#Argument_Options
SSomeone can tell me what's the best way to get a post using it's id?
I'am using this:
$query = query_posts('post_id='.$_GET['php_post_id']);
global $post;
foreach ($query as $post):
do stuff...
This is returning an array with all post
get_post( $post_id, $output );
So in practice will look like:
$my_id = 7;
$post_id_7 = get_post($my_id);
Further reference about the post's parameters and fields, here: http://codex.wordpress.org/Function_Reference/get_post
Update: It's the best practice when you need to get a single post by id, no cicles required.
Change post_id= to p=.
$setQuery = 'p='.$_GET['php_post_id'];
query_posts($setQuery);
Click in this link to see: Retrieve a Particular Post
If you are looking to get a single post with an ID you already know or getting from another source, i'll suggest the below code.
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'p' => $id, // id of the post you want to query
);
$my_posts = new WP_Query($args);
if($my_posts->have_posts()) :
while ( $my_posts->have_posts() ) : $my_posts->the_post();
get_template_part( 'template-parts/content', 'post' ); //Your Post Content comes here
endwhile; //end the while loop
endif; // end of the loop.
You can create a query like so:
$rd_args = [
'ID' => $postId
];
$query = new WP_Query($rd_args);
And then you can retrieve the post from the query. Or set it to the global query and loop over it:
$GLOBALS['wp_query'] = $query;
while ( have_posts() ) : the_post();
Here is the code to fetch post using query_post if you know the ID.
<?php
$my_query = query_posts('post_id=111&post_type=parks'); // in place of 111 you need to give desired ID.
global $post;
foreach ($my_query as $post) {
setup_postdata($post);
the_title();
the_content();
}
?>
As as title, how to prevent wp not to encode html in a post?
currently i just need to prevent '&' change to '&'
The result need to be looks like on editor with html tab mode selected.
$content = $wpdb->get_row("SELECT post_content FROM $wpdb->posts WHERE ID=xxx");
$content = str_replace('amp;','',$content->post_content);//remove amp;
$wpdb->query("UPDATE $wpdb->posts SET post_content = $content WHERE ID = xxx;");
but that code still encode the html.
update:
and also how to implement content filter(prevent to encode some text) in collaboration with wp_insert_post() function
update[SOLVED]: stackexchange
$content = get_post_field('post_content', XXX, 'raw');
$content = str_replace('amp;', '', $content);
$wpdb->update( $wpdb->posts, array( 'post_content' => $content ), array( 'ID' => XXX ) );
<?php $content = htmlentities( html_entity_decode($content) ); ?>
This code will print HTML tags, and then decode HTML entities (like & to &).
Oh, and with wp_insert_post(), do:
// Create post object
$my_post = array(
'post_content' => $content,
'post_id' => POST_ID # update the post with the same ID as POST_ID
);
You can use the escape function as a shortcode:
function escape_html_func( $attrs, $content = "" ) {
return htmlentities( html_entity_decode($content) );
}
add_shortcode( 'escape', 'escape_html_func' );
like [escape]<span>Hello!</span>[/escape] in blog posts. Is this what you meant by content filter?