wordpress - manually set the author of a post in php - php

I'm using a wordpress theme that allows users to create posts (they don't need to be logged in). The problem is that when the posts are displayed, <?php the_author(); ?> always shows as "admin". The post-creation form has a name field that I can get at in functions.php, but how can I manually change the author of the post to the name that was entered in the form field?

Without seeing the context, I can only offer a step in the right direction. Within a plugin or functions.php in your theme, you can call on the wp_insert_post_data filter to change the data that will be inserted as your post. For instance, if you wanted every post to be attributed to author with ID 5, you could do something like:
function my_change_author( $data , $postarr )
{
$data['post_author'] = 5;
return $data;
}
add_filter('wp_insert_post_data' , 'my_change_author' , '99', 2);
I've done something like this before where I have front end forms that I capture as posts and automatically assign to a predefined author that I call Web Monkey or something like that ;) Check out this page for more: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data. This filter is really powerful in that you can alter your data that will become your post in anyway before it hits the database.

Have you checked what the_author_meta() contains? WP has to attach a userID to each post, so chances are it's assigning user 1 to those posts regardless of what they're entering into the box. Without seeing the custom function or knowing what theme you're using to test, it'll be pretty difficult to troubleshoot.

Related

How can I display posts in wordpress based on page category?

I would like to know how I can display single posts for a specific page.
ex. page url
http://www.yourblog.com/services/single-service-post-title
I tried creating "single-service.php" but it redirects me to my static front page.
I have setup a page-services.php and that one works fine, but that page is designed to only show a summary of posts.
It's not entirely clear what you're looking for, since pages can't have single posts. They're two different things.
If you're looking for a way to create a template for a single custom post type, you can view an interactive version of the hierarchy here.
You have to name the file based on the id of the custom post type. So if the id of the post type is 'service', then 'single-service.php' would display the single post where post type equals service.
You can also place logic into your 'single.php' to say something like
if(get_post_type(get_the_ID()) == 'service'){
// DO SERVICE-SPECIFIC STUFF HERE
}
This is usually for single custom post types that don't have their own template, but it can also be used as a fallback if that's something you're worried about.
Then you can use an else for regular post stuff and put stuff common to both outside of the if/else.

Wordpress: How to ping URL from custom field on publish

i have a custom field for source-links. The URL inserted in this custom field should get pinged once the post gets published. I know there is a dedicated trackback field but i want the URL from the custom field automatically added.
As far as i understand, $add_ping is exactly that.
http://codex.wordpress.org/Function_Reference/add_ping
My problem is: i don't know where to add it. I imagine if i'd write this into my theme where i display the source-link, the source gets pinged everytime the post gets visited.
So whats the proper way to add an url to get pinged on publish?
To be clear: it is not a service that i want to get pinged. More like if you'd insert a link to post B into post A's content. Once post A gets published, post B (or its blog) gets pinged. I want to insert the link into a custom field of post A, instead of its content area.
I think Felipe is right. You could try something like this:
function doCustomPing ($post_id) {
$uri_to_ping = get_post_meta($post_id, 'FIELDNAME', true);
add_ping($post_id, $uri_to_ping);
}
add_action ('publish_post', 'doCustomPing');
So every time a post if published (that includes if its edited) then a hook into publish_post will run the doCustomPing function. If you're putting this as part of a theme drop the above code in your functions.php file. Put the name if your custom field where it says FIELDNAME.

Wordpress Edit Post Hook to load custom data

I have an meta_box in the post creation screen. As soon a user click on a specific category it will be displayed. For saving the information in the meta_box and post title and post content I use the hook post_updated. All is going to save fine in the database -- post data in wp_posts and the data in the meta_box into a special created table.
But sometimes the user want to edit this post from this 'special 'category. In this case the meta_box with the inserted information should be loaded and be editable. I know that I need to fill in the fields manually, but I need to know which hook is called when the user presses the edit link. An idea was, check for the url and if a parameter edit is found call my edit routine. But it looks like a dirty thing for me.
The codex is not very helpful. I tried the following hooks:
edit post, publish post, save post, wp insert post
But they are called after I pressed the actualize button.
BR,
mybecks
You don't have to use any hook for this, you just need to use following lines where you are generating your meta_box html
global $post;
now you have access the $post object when you are editing
get_post_meta($post_id, $key, $single);
use above function to get the value of particular meta and set it to your html as required.
for reading more about get_post_meta, goto http://codex.wordpress.org/Function_Reference/get_post_meta

Add link in custom post-type - Wordpress

I have a custom posttype in Wordpress 3. I would like to every post of this type to have the option to add a link. Basicly this will be a link that refers to another page/post whatever on the site.
There should be only one link for each post of this type. And i would then need to extract this link in my template files. Basicly im creating a post-type "Slideshow" And each slide-item should be connected to one page or post. So when you click a slide you will be taken to the defined page.
i know i can do this by using a custom-field. But then i would need to enter the whole url every time. I would like a feature similar to that of the wordpress WYSIWYG editor link button. So i can add a link to "existing content" easy.
Anyone know of any tutorials or similar on how to do this?
Thanks!
bit surprised no one has mentioned this plugin "Related Links"
Wordpress plugin Related Links
It adds a metabox to your edit forms. You can link to related content or put in external URL.
It allows links to posts, pages, media AND Custom post type - check its type on the Settings page of the plugin after install. It uses a similar search/browse facility to the normal WYSIWYG link insert feature!
What post-types does it apply to? If you are using on a custom post type, then check all the post-types you want to be able to link to - it still shows the box on this post-type itself.
It can accept multiple links, but one will work of course. Then you use the get_related_link() function in your template to output this and format as you like.
If you just want to make a link between the two posts, rather than insert it in your post content, I'd recommend the Posts 2 Posts plugin. It'll allow you to create links between posts without having to remember the full URL.
Edit:
I haven't used it, but I suspect the cardinality argument should help you limit the number of links - see the wiki.
And you can certainly access the connections in your templates - I have. Once you've registered your connection type, you can just call get_connected:
$venue_details = p2p_type( 'exhibition_to_venues' )->get_connected( $post->ID, array(
'posts_per_page' => -1,
'connected_orderby' => 'order',
'connected_order' => 'ASC' )
);
It seems to me that the best method to accomplish this would be to add a custom meta box to all posts and post types (see here:http://themefoundation.com/wordpress-meta-boxes-guide/). Within this meta box, you can simply query all posts that you would like to include in a dropdown. You can then select the post from that dropdown, obtain the ID of the post selected (use as data attribute in the option field), and then return the permalink for that ID. This will then allow you to simply select the post, rather than having to know the actual URL each time.
Another method would be to attach a piece of unique data to x post (most likely utilizing custom fields), and then also attach it to y post. In this way, you could use a function to automatically append the link to the displayed post. You would do this by querying the posts in the database, match the custom data, and if matched, display the link to that post. This would allow the entire thing to be automated, and you wouldn't even have to select anything. In my opinion, the title field should actually be sufficient for this, since both posts are different post types (you should be able to title them the same), and would likely make your query a bit easier/shorter since you would simply need to find the post that matches the title, and then link to the permalink.
I can elaborate on all of the above further, with code samples as well, but in my opinion, the question is slightly too vague to write a custom script example for this scenario.
I would hope the above would be sufficient to get you going.

Wordpress hook to pre-populate the content field on post-new.php?

I'm writing a plugin which will help administrate a question-answer database. 'Candidate' questions are stored in a custom DB, and I'm working on a function by which a user can promote a db question to a fully fledged knowledgebase article (custom post type).
I'd like to populate the content field on post-new.php based on the content of my $_POST data, so a user can click a 'promote' link associated with a candidate and have the information already there.
What's the appropriate filter hook to use for this, or is there a better way?
*Edited title for clarity
Not exactly sure I have grasped your point, but if the difference between an Q and A entry and a KB entry is more than just a flag in the table, eg it is a largely a copy operation from one table to another: Couldnt you simply use Mysqls INSERT INTO ability? http://dev.mysql.com/doc/refman/5.5/en/insert-select.html
which is triggered by the user posting back just the id of the Q and A article?
It turns out the filter hook I was looking for is the_editor_content, which will (as its name suggests) let me manipulate the content that gets placed in the editor.
I think you need to hook into default_content like this
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "This is my default content!";
return $content;
}
as stated here

Categories