Wordpress ACF how to display something other than post title for custom relationship field in Admin - wordpress

I have custom post types for which having a 'title' field doesn't really make sense, so the post type has none.
Problem is that when you use a post type in a relationship, the user interface for picking which posts to relate wants to display the title.
In the box where you are choosing posts (clicking them and they move to the right pane), can a field other that 'title' be displayed?

It's doable. I would use a filter hook provided by acf plugin 'acf/fields/relationship/result/name=related_posts'. Take a look at the following code:
add_filter( 'acf/fields/relationship/result/name=related_posts', 'my_custom_title', 10, 2 );
function my_custom_title( $title, $acf_array ) {
// You could remove the $title altogether or you could add something meaningful to it such as time stamp or anything that would make sense!
return $title . "Adding somthing meaningful to the title!";
}

Related

Custom Columns Don't Display Anything

I have added a new column to a custom post type to display the post's ID. This works for the WordPress core post type, but not for my custom post type. I have tried using the manage_{post_type}_custom_column hook and just applying it to all posts, but neither works.
It DOES add the custom column headers, but I can't populate them with anything at all when viewing the custom post type.
This is what it looks like when viewing the custom post type
and
this is what it looks like when viewing a regular core post.
// Add post ID column to use an order ID in all posts view.
add_filter('manage_posts_columns', 'oms_order_id_header');
add_action('manage_posts_custom_column', 'oms_order_id_column', 10, 2);
function oms_order_id_header($columns) {
//Remove title column
//unset($columns['title']);
//Add new columns
$columns['order_id_header'] = 'Order ID';
$columns['customer_header'] = 'Customer';
$columns['author'] = 'Owner';
return $columns;
}
function oms_order_id_column( $column, $post_id ) {
if ($column == 'order_id_header') {
echo $post_id;
}
}
This turned out to be an issue with the post type being set to hierarchical. Hierarchical post types need to be targeted by a different action hook than the one used here.
Instead of manage_posts_custom_column, hierarchical post types need to use manage_pages_custom_column.
I just tried you code and it seems to be working perfect on my WordPress installation in both cases: Custom Post and Post.
Maybe your error is happening because your posts are drafts? I don't think so but maybe. (I tried your code also with drafts on my installation and it worked). Here is the screenshot:
Try printing "hello world" instead of the $post_id to check if it prints in all cases.

Add specific words to WordPress custom type slug

I have a WordPress installation with a custom type (places) wich produces this permalink structure:
http://example.com/places/the-first-site/
And I would like to show all my sites like this:
http://example.com/places/visit-the-first-site-and-enjoy/
where 'visit' and 'and-enjoy' would be always those specific words (constants).
Even better I would like to put a custom taxonomy I have (year) as a metadata
places/visit-the-first-site-and-enjoy-1985/
I can access the DB and modify the post name of all post, but I would have to do for the new post also, and I'm looking for some automated rule, but can't find how to do.
Maybe some rewrite rule, but I don't know how to do it.
Any ideas??
You do not have to edit anything in your database nor any codes in your WordPress installation; you can achieve that directly from your WordPress administration panel.
From your Dashboard, click on the Permalink sub-menu under Settings; there, you will have to select the Custom Structure option and set it as follow in order to achieve your desired effect:
/places/visit-%postname%-and-enjoy-%year%/
Please note: here, we made use of both %postname% and %year% Structure Tags so as to get names of posts with their corresponding year of publication respectively.
Don't forget to click on the Save Changes button on the page in order to effect your changes.
... Read more on Permaklinks (for general knowledge).
For a custom post type and taxonomies as expressed further in comments, you will need a custom solution which will require a little bit of coding and or tweaking, depending on your abilities; you may use this handy plugin (Custom Post Type Permalinks) from the WordPress.org Plugins repository.
This posts should equally be of great help to you, to getting started and understanding further, should you chose to code.
You have différent hook like save_post or pending_to_publish, where you can set or change the "post_name" (that's corresponds to the permalink slug).
To Add specific words to WordPress custom type slug You have to Register custom rewrite rules.
suppose this is Your URL http://example.com/places/the-first-site !
and you have a post type places.
function add_rewrite_rules()
{
$wp_rewrite->add_rewrite_tag('%places%', '([^/]+)', 'places=');
$wp_rewrite->add_rewrite_tag('%state%', '([^/]+)', 'state=');
$wp_rewrite->add_permastruct('places', 'places/visit-%state%-and-enjoy/', false);
}
function permalinks($permalink, $post, $leavename)
{
$no_data = 'no-data';
$post_id = $post->ID;
if($post->post_type != 'story' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$state = get_post_meta($post_id, 'location', true);
if(!$state)
$state = $no_data;
$permalink = str_replace('%state%', $state, $permalink);
return $permalink;
}
add_action('init', 'add_rewrite_rules');
add_filter('post_type_link', 'permalinks', 10, 3);
put this code in your custom post type plugin. and if get page not found error please change your permalink setting. or make a custom template file to show this post.

Wordpress; Vantage theme & ACF plugin?

I have repeatedly been trying to implement the Advanced Custom Fields (ACF) plugin in the Vantage theme (from AppThemes), but without any success. I've had extensive contact with the admin of ACF, on this subject, but unfortunately we did not succeed. He advised to ask here, maybe seek for a (paid) developer, to solve this problem.
Ok, so what am i trying?
I have created a custom field group and want to implement that field group within the listing form of Vantage. To do so i have read several docs, including: http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/. To be complete, based on this i did the following in the Vantage theme folder:
I created 2 custom field groups, with the id's: 88 & 139.
In Wrapper.php i added the code: <?php acf_form_head(); ?>
In form-listing.php i created 2 custom hooks, 'product_custom' and 'product_custom2': <?php do_action( 'product_custom' ); ?> <?php do_action( 'product_custom2' ); ?>
In fuctions.php i created 3 functions:
add_action( 'wp_print_styles' , 'my_deregister_styles' , 100 );
function my_deregister_styles() {
wp_deregister_style( 'wp-admin' );
}
add_action( 'product_custom' , 'productfields' );
function productfields() {
$options = array(
'field_groups' => array('post' => '88' ),
'form' => false,
);
acf_form( $options );
}
add_action( 'product_custom2' , 'productfields2' );
function productfields2() {
$options2 = array(
'field_groups' => array('post' => '139' ),
'form' => false,
);
acf_form( $options2 );
}
This actually made the custom field group show up in the listing form of Vantage. However the following things keep going wrong:
Both field groups have a WYSIWYG field. However for some reason the WYSIWYG buttons and media button stop working
I cannot fill in any text in the first WYSIWYG field. Only the second one works for that matter.
No data is stored at all after saving the listing form. On advise of the ACF admin, i tried the following in the acf-master/core/api.php file:
// run database save first
if( isset($_POST['acf_save']) )
{
$txt="Hello world!";
echo $txt;
die();
However the string does not display after saving the listing form. So the if statement is not used...
4. To display the dataon the frontend, once they are saved, I guess the default wordpress codex can be used..
I tried to be as complete as possible;) Is there anybody who can help me any further? Paid assistance is also negotiable..
Thanks a lot in advance!
Robbert
I have succeed on implementing ACF with vantage themes.
I add ACF form at vantage listing form and combine the vantage form with ACF form. with one button.
The data has been saved to database and can be called to displayed in listing area. Only add image button is not working from front-end but in back-end the button is working.
Adding <?php acf_form_head(); ?> to wrapper.php
Do this tutorial front end form help
Eliminate default vantage submit button in form-listing.php
Add this code in ACF api.php in function acf_form_head()
// allow for custom save
$post_id = apply_filters('acf_form_pre_save_post','va_after_create_listing_form', $post_id);
That's it, hope it work in your site.
Is that you want when someone visits the website that they are able to send info via the page they visits eg www.yoursite.com/listing/listing-name like probably an email or contact for more info relating to that business?
If not then you can simply add in the ACF data from the back-end i.e. dreamweaver etc onto single-listing.php and use the ACF tutorial on working with fields.
Hope this helps somewhat
Cheers

Wordpress widget for post specific content in sidebar

I am looking for a wordpress plugin that will allow me to add a paragraph to the sidebar that is specific to the blog post. I would need to be able to add that text when creating the post. Is there something out there like that? I have been unsuccessful in searches.
Thanks
This can be easily solved using Custom Fields, the Text Widget and a Shortcode.
This bit of code goes in the theme's functions.php or, preferable, within a custom plugin.
1) Enable shortcodes for the Text Widget
add_filter( 'widget_text', 'do_shortcode' );
2) Define the shortcode, read comments for details
add_shortcode( 'mytext', 'so_13735174_custom_text_widget' );
function so_13735174_custom_text_widget( $atts, $content = null )
{
global $post;
// $post contains lots of info
// Using $post->ID many more can be retrieved
// Here, we are getting the Custom Field named "text"
$html = get_post_meta( $post->ID, 'text', true );
// Custom Field not set or empty, print nothing
if( !isset( $html ) || '' == $html )
return '';
// Print Custom Field
return $html;
}
3) Add a Text Widget in the desired sidebar.
Leave the title empty and put the Shortcode in the content: [mytext].
4) Now each page or post with a Custom Field named text will have its value printed in the Widget.
5) The $html can get fancy and multiple Custom Fields can be used.
This isn't something that I've ever personally done, but try this.
Summary: You will add the a paragraph using a custom field, then display it in a widget.
Details:
First, make sure custom fields are enabled. Edit a post, then click
the "screen options" at the top right of the page. If "Custom
Fields" isn't checked, check it. You should now see a custom field
area below the post editor.
Come up with a name for your custom field. Perhaps
"extra_paragraph". Now put that in the "name" field in the custom
field area.
Write your paragraph in the "value" field the custom field area.
Install the Custom Field Widget plugin, set it to display this
new "extra_paragraph" field. (widget appears to be untested with newer versions of Wordpress so cross your fingers!)
Now when you write or edit posts you should see this "extra_paragraph" field as an option in the "name" dropdown.

Multiple Authors on same post in Wordpress

I have a friend who has asked me to build a site for him and two friends to write movie reviews. I'm pretty good with Wordpress, so it was the obvious choice for the site. The only difficulty I have is that they each plan to write a review on the same movie, and I can't think of how to achieve multiple authors in one post.
I've checked out a few plugins such as Co-Author Plus which allows multiple authors credited to the same post, but it doesn't provide the functionality for keeping each author's content separate.
The only solution I can think of is to use custom fields, but I would prefer if the authors can use the main content editor for their reviews. Any help is greatly appreciated!
Like I said, it should be better to have 1 review = 1 post.
So, I think the best way to achieve this should be to create post types :
movie
review, with a movie reference field
And modify post template to display movie and associated reviews on the same page.
An alternate solution should be to use taxonomy to handle movies and attach post to them.
I ran into the same problem with trying to figure out how to do this as well and I followed soju's advice and came up with a solution. There might be a better solution to this, but this is how I went about it. Me and my friend are doing an anime review blog and both me and him will be writing a review on the same anime.
I first created two post types:
anime: main page on the specific anime, like the description, pictures, etc.
reviews: the author's review on the anime. the options i have enabled on here are the editor, the title, and the author. Along with the associated anime taxonomy. That's all that's needed here
Then I created a taxonomy for anime titles so when a user needs to write a review on an anime that hasn't been added as a review yet they can add the title into the taxonomy.
I associated the taxonomy to both post_types and wala! That's pretty much all you need.
So now when you want to write a new review for a new anime you add a anime post type first and write down what the anime is about and include pictures etc. Add the title to the taxonomy and check it. After that you then create a new post of the post type reviews and write your review, remember to check the correct title in your taxonomy for what anime this is going to, then you are ready to go!
Problem 1: How do I include this into my loop?
Well you don't want to include both post types in your loop you just want to include posts and the other post type anime in your loop so you do the following in your functions.php file:
function include_custom_post_types( $query ) {
global $wp_query;
// Get all custom post types
$custom_post_type = get_query_var( 'post_type' );
// Get all post types
$post_types = get_post_types();
// If what you are getting is a category or a tag or that there are no custom
// post types you just want to set the post types to be the current post types
if ( (is_category() || is_tag()) && empty( $custom_post_type ))
$query->set( 'post_type' , $post_types );
// Set the custom post types you want to ignore
$ignore_types = array('reviews');
//Unset the post types that are gonna be ignored
foreach($post_types as $key=>$type)
{
if(in_array($type,$ignore_types))
{
unset($post_types[$key]);
}
}
// Set the post types for the query
if ( (is_home() && false == $query->query_vars['suppress_filters']) || is_feed())
$query->set( 'post_type', $post_types);
return $query;
}
add_filter( 'pre_get_posts' , 'include_custom_post_types' );
Problem 2: How do I display the reviews?
I solved this by creating another single.php file and renamed it to single-post_type_name.php. So in this case i created a single-anime.php file for my post type anime. Then in place of the contents i want to get all the reviews for this specific anime so I added the following within the file in the main content area:
<?php
//You grab the taxonomy that you have selected for this post
$terms = wp_get_post_terms(get_the_ID(), 'animes_reviewed');
// This is the args array for the criteria that the posts need to be in
$args = array(
// This is the post type of where your reviews are at
post_type' => 'reviews',
// this is for searching the taxonomy usually it's
// taxonomy_name => checked_taxonomy
'anime' => $terms[0]->name,
'post_status' => 'publish'
);
// Grab the posts
$posts = get_posts($args);
//Here I echo out the information for debugging purpose, but
//Here is where you can do HTML to display your reviews
foreach($posts as $post)
{
echo($post->post_content);
the_author_meta( 'nickname', $post->post_author);
}
?>
You can do a lot more with this adding more taxonomies etc. I have actually implemented an episode review by just adding a taxonomy and adding a criteria to look for in the post section. Hopefully this will help you out, might be a bit late though :( Thanks soju for recommending the custom post types!

Resources