Replace Blog Post Author link with Team Member link (CPT + ACF) - wordpress

I'm building a website that has "Team Members" section and "Blog" section. Team members are a custom post type.
The client requested that the links to authors in blog posts should point to associated Team Member page instead of the default author page in WordPress (basically they don't want to have default author pages at all).
I've found this solution offered by #Damocles - Use "Team Members" custom post type instead of Author for blog posts
Basically the solution proposed by him was simple and exactly as I thought initially of tackling this issue:
Create a "Post Object" ACF field and set it to filter through "Team Member" custom post types
Attach this field to User accounts
Go to user profile and choose the correct Team Member from drop down menu
Then use a filter in functions.php to automatically replace the author link everywhere with associated Team Member url
Makes sense but unfortunately, it doesn't want to work on my website. I even used the same name for ACF field as he did and used the exact same code in functions.php:
add_filter( 'author_link', 'team_author_link', 10, 3 );
function team_author_link( $link, $author_id, $author_nicename ) {
$team_post_id = get_field('team_post', $author_id);
// if the team post is set, get the permalink to the team post:
$team_link = get_permalink($team_post_id);
$link = ($team_link !== false) ? $team_link : $link;
return $link;
}
The author link DOES change, BUT instead of pointing to the associated Team Member page, all author links point to the currently opened blog post URL. I don't know, maybe my theme is overwriting the query or something, so the URL to the custom post type cannot be achieved from blog post view?
Can someone help me achieve it, please? I want to attach a Team Member (custom post type) to user account in WordPress and replace the author link through functions.php to the associated Team Member page url.

It's because you are missing a prefix, to tell ACF that it should be looking on a user.. - try this
$team_post_id = get_field('team_post', 'user_'.$author_id);
Docs are here : https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/

Related

Replacing Wordpress Category with Static Page?

I'm rehashing a Wordpress question from 4 years ago that had an approved answer, but that solution no longer works in the current version of Wordpress. The original question: Make Wordpress use the page instead of category
Background (copied from originally approved answer):
example.com is the domain
I have a WP page called "foobar" with content
I have a WP post category called "foobar"
I have a WP post entitled "fun things to do with foobars", and the category is set to "foobar"
Expectations
When I go to example.com/foobar, I want to see the page about foobars, not a WP category page that shows all blog posts with that category.
When I go to the blog post about fun things, the URL is example.com/foobar/fun-things-to-do-with-foobars/
The solution at the time
There appeared to be a work-around with a custom permalink structure and using "." (no quotes) as the category base. That solution does not work with the current version of Wordpress.
There were also a couple of answers that suggested various plugins. I'm not overtly opposed to that route, but the suggested plugins at the time seem to have been affected by the same update that negated the accepted answer.
To load page with the same slug as of category name you could use following code snippet that replace category with Page.
function wpa_alter_cat_links( $termlink, $term, $taxonomy ){
if( 'category' != $taxonomy ) return $termlink;
return str_replace( '/category', '', $termlink );
}
add_filter( 'term_link', 'wpa_alter_cat_links', 10, 3 );
Be aware with the code. You should have pages for all your categories otherwise you will get 404 for those categories which have not created pages.
After adding above code into your activated theme's functons.php file please flush the permalink. You can do that with visiting settings -> permalinks -> click save changes
Hope it works for you!

Wordpress post to user relation ship

Is there any way to connect post to specific user in wordpress. Is there any plugin available. Or any one know the code for doing that.I have a custom post type stories. When adding stories i need to chose the corresponding users from user list. Please help
Install Post2post https://wordpress.org/plugins/posts-to-posts/
Then in your function.php write this code
p2p_register_connection_type ( array(
'name' => 'releated_user',
'from' => 'story',
'to' => 'user'
) );
here story is your custom post type slug. related_user is just a connection name.You can name it as what you like.
Then in your post type section you can see an option for selecting corresponding user.
In the custom post type setup, you first need to ensure that this post type allows for authors to be set.
This comes in from the arguments when registering the post type.
$args = array('supports'=>array('author'=>true));
If you have an admin account, you can set who the author is using the quick edit function or by allowing to see it under screen options on the full edit page.
Other than that, you can make your own complete post meta box to allow for multiple authors. I cant see a plugin on the wordpress plugin directory so creating your own to fit your needs and wants will be your best bet.

Create a blog post from content in another post type

The Wordpress site I'm working on has a section for "News" (which is the regular blog/posts) which will be used for any news the company has to write about. Then I have a custom post type for Promotions, which has it's own page.
I want the client to be able to add his promotion content through the custom post type, which is going on the Promotions page, however I'd like this content to also be "cross posted" into the blog/news without forcing the client to write it up twice.
Is there a way to do this? Thanks.
Just a note: The reason I have the promotions as a custom type on it's own instead of just having them do it all from the blog is because I needed custom fields that would be unnecessary for any other kind of blog post.
Two options:
1) Use the Shortcode API
And in your cross-post you'd add the shortcode [crosspost id="POST-ID"]. Where POST-ID corresponds to the numeric ID of the other post (post type). Instead of ID, the title could be used, see the function get_page_by_title.
Create your own plugin for this. Add a sample shortcode from the Codex and use the function get_post to get the contents of the cross-post.
2) Use Advanced Custom Fields plugin
With it, adding meta boxes with custom fields is a breeze. And it has a Post Object field that's basically a Cross Post functionality.
You could do it a lot more simply by adding a filter to wp_insert_data(). For example, in your theme's functions.php file add the following:
add_filter('wp_insert_post_data', 'post_to_other', 99, 2);
That filter will then run anytime you add a new post. In the function post_to_other(), you look to see what type of post is being submitted. If it's a promotion, then insert a second copy as a News item.
function post_to_other($post_id, $post){
/** check $post to see what type it is, if it's a promotion */
if($post->post_type == 'promotion'){
$second_post = array(
'post_type'=> 'post',
'post_title'=> $post->post_title,
'post_name' =>$post->post_name,
'post_content'=> $post->post_content,
'post_author'=> $post->post_author,
'post_status'=> 'publish',
'tax_input'=> array('taxonomy_name'=>array('news'))
);
wp_insert_post($second_post);
}
}
I'm running out the door so I don't have time to double check the exact code but that's the basic structure of it. The tax_input bit is optional, lets you specify a category if you want. You'll probably need to tweak it a bit but that's the basics.

add custom field template to theme admin page

in WP admin how to add the custom field template plugin to a theme page?
it automatically shows in posts and pages but i want this in the theme page. the theme am using is from iwak "creations" portfolio page.
what files do i need to modify to add this?
It's very hard to say what you need to modify without being able to look at the code. Being a premium theme, we can't just download it and take a look.
Having said that, the theme may use the WordPress custom post type functionality. Search the code for a call to the register_post_type function. If it's used, you may be in luck. Either
add 'custom-fields' to the supports argument in that call, or
call add_post_type_support after the post type is registered. The $post_type parameter will be the first value passed to the register_post_type function, and the $supports parameter will be 'custom-fields'.
Daniel, are you using this Custom Post Type Plugin - http://wordpress.org/extend/plugins/custom-field-template/screenshots/? I've used it before, and the guy who created it is Japanese, so his personal page isn't very useful as far as support for english goes.
I had some trouble with this at first. But what I think you're trying to do is add the custom fields to your new pages you've created, correct?
It's not very straightforward, but once it works it's pretty awesome.
Basically, when you set up the plugin you create these different "Custom fields," right? Well part of that should look like this:
[Custom Field Name]
type = textarea
rows = 4
cols = 40
tinyMCE = true
htmlEditor = true
Ok, so once you've created those "Custom fields" keep note of the part in brackets. You'll add this to your template pages:
<?php getCustomField('Custom Field Name'); ?>
Now when you enter the info in the pages or posts, the content should appear as you've entered it.

Syndicating custom fields in Wordpress via RSS

I wonder if I could ask a Wordpress / RSS question I could't find an answer for around here,
Trying to syndicate posts via RSS in Wordpress using the FeedWordpress plugin as an RSS aggregator, each post in the original blog includes five custom fields that are important for its Theme functionality (the original and syndicating / receiving blog using the same theme).
The original RSS2 feed doesn't include these custom fields apart from one, being enclosure, that is defined in the default rss feed template (function in WP rss_enclosure).
This is written in the original feed such as:
<enclosure url="http://www.samplevideourl.flv" length="18554755" type="video/x-flv" />
Tried to add the rest of the custom fields modifying the rss2-feed.php template so they show at the end of each segment in the current RSS2 feed, now they are included as for example:
...
<ratings_average>0</ratings_average>
<views>5</views>
</item>
However, if I update the syndicated posts, or delete the posts and fetch the modified feed again with feedwordpress, none of these show in the syndicated posts.
Is there a way to include these custom fields so they are recognized by feedwordpress?
Basically need to syndicate the same format of the post as the original including all its custom fields.
Many Thanks
Carlos
There is a thread that covers this: https://wordpress.stackexchange.com/questions/3801/add-custom-fields-to-custom-post-type-rss
I've condensed the answers there to reflect the later improvements (thanks MikeSchinkel, prettyboymp and Acts7).
Add this to your theme's functions.php:
/* IN ORDER TO VALIDATE you must add namespace */
add_action('rss2_ns', 'my_rss2_ns');
function my_rss2_ns(){
echo 'xmlns:mycustomfields="'. get_bloginfo('wpurl').'"'."\n";
}
add_action('rss2_item', 'yoursite_rss2_item');
function yoursite_rss2_item() {
if (get_post_type()=='my_custom_post_type') {
$fields = array( 'field1', 'field2', 'field3' );
$post_id = get_the_ID();
foreach($fields as $field)
if ($value = get_post_meta($post_id,$field,true))
echo "<mycustomfields:{$field}>{$value}</mycustomfields:{$field}>\n";
}
}
This will add all custom field names and values to the site's main feed.
Note, for custom fields with more than one value, a modification is necessary as the above will only work for single value fields, not arrays.
So,
On your Master site (where you are syndicating FROM) you add the above function.
On the Slave site (where you are syndicating TO), assuming you have FeedWordPress installed, go to "SYNDICATION" ->
Click on the name of the RSS feed
Go to Custom Feed Settings and plug in the pieces

Resources