Skip Navigation

[Fermé] Custom fields as Post title

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 11 réponses, has 3 voix.

Last updated by Eric Il y a 9 années et 5 mois.

Auteur
Publications
#8933

I'm working on a real estate site... The client wants/needs to store the property addresses in the database as separate pieces: Street Number, Street Name, City, State, Zip. That's pretty easy to do with custom fields. The problem is, they also want the property address to function as the post title, and they don't want to enter the data twice: once in the main post title field as a single string, and again in the custom fields. Is there any way to build a string from custom fields and inject it into the post title?

#8935

Amir
Supporter

I don't get it. If you already enter the address as the post name, why do you also need it as a custom field? When you display that property, you can show the 'title' field anywhere in the template. It doesn't need to be a custom field for that.

#8976

Thanks for replying, Amir...

The client has an existing site (not WordPress) with a database of over 1000 property listings, and in their current admin the address is entered into separate fields: Street Number, Street Name, City, State, ZIP. I'm trying to recreate much of their existing admin using WordPress and Types.

They want to maintain this method of entering their addresses, in multiple parts, not as a single line as the post title, but in a series of custom fields. BUT, they want the post title to be comprised of the property address, without having to enter the data into the post title as well as the custom fields. I'm wondering if there's a way to dynamically assign a post title based on concatenating data from a series of custom fields. Is that any clearer? I'm more of a front-end designer/developer than a heavy-duty php guy, so thanks for your patience. 😉

#8977

I believe I'm getting close with this, but I think I'm not getting the custom field names correct, or the post_type name for that matter.

add_filter('title_save_pre', 'save_title');
function save_title($my_post_title) {
	if ($_POST['post_type'] == 'wpcf-listing') :
		$street1 = $_POST['wpcf-street-1'];
		$house_num = $_POST['wpcf-house-number'];
		$new_title = $street1 . " " . $house_num;
		$my_post_title = $new_title;
	endif;
return $my_post_title;
}

add_filter('name_save_pre', 'save_name');
function save_name($my_post_name) {
	if ($_POST['post_type'] == 'wpcf-listing') :
		$street1 = $_POST['wpcf-street-1'];
		$house_num = $_POST['wpcf-house-number'];
		$new_name = $street1 . " " . $house_num;
		$my_post_name = $new_name;
	endif;
return $my_post_name;
}
#9037

bruce
Supporter

Languages: Anglais (English )

Timezone: Australia/Perth (GMT+08:00)

Hi Eric,

What is the custom post type slug? It won't have a "wpcf-" prefix unless you specifically entered one.

Best regards,
Bruce.

#9045

The post slug is "listings"

I removed the wpcf- prefix, but no luck.

if ($_POST['post_type'] == 'listings') :
#9051

bruce
Supporter

Languages: Anglais (English )

Timezone: Australia/Perth (GMT+08:00)

Hi Eric,

Are you sure your code is being called. I can't find anywhere in WP source where the 'title_save_pre' and 'name_save_pre' filters are being called.

You could try the save_post action.

Best regards,
Bruce

#9055

As a test, I edited the code to look like this:


add_filter('title_save_pre', 'save_title');
function save_title($my_post_title) {
	if ($_POST) :
		$street1 = 'hardcoded street';
		$house_num = '9999';
		$new_title = $street1 . " " . $house_num;
		$my_post_title = $new_title;
	endif;
return $my_post_title;
}

add_filter('name_save_pre', 'save_name');
function save_name($my_post_name) {
	if ($_POST) :
		$street1 = 'hardcoded street';
		$house_num = '9999';
		$new_name = $street1 . " " . $house_num;
		$my_post_name = $new_name;
	endif;
return $my_post_name;
}

I then edited and updated a listing; my post title changed to "9999 hardcoded street" and my permalink to .../listings/9999-hardcoded-street. Cool!

So, I think I'm getting close. I'm just missing something... My php skills are fairly rudimentary, as I've mentioned, so I really appreciate the help.

Thanks,
Eric

#9057

bruce
Supporter

Languages: Anglais (English )

Timezone: Australia/Perth (GMT+08:00)

Hi Eric,

try changing your function to the following to see what post information you are getting:

return $_POST['post_type'] . $_POST['wpcf-house-number'];

Best regards,
Bruce.

#9058

That changed the post title and permalink to "listings" but didn't pick up the wpcf-house-number. Getting closer.
thx
E

#9060

bruce
Supporter

Languages: Anglais (English )

Timezone: Australia/Perth (GMT+08:00)

Hi Eric,

It could be $_POST['wpcf']['house-number'] to get the custom field.

Best regards,
Bruce

#9062

BINGO! Thank you, Bruce!

I have a feeling this may come in handy for anyone else looking to customize their post titles based on custom field data, so I'm including the code (which works for my custom post type) here...

add_filter('title_save_pre', 'save_title');
function save_title($my_post_title) {
	if ($_POST['post_type'] == 'listings') :
		$street1 = $_POST['wpcf']['street-1'];
		$house_num = $_POST['wpcf']['house-number'];
		$new_title = $house_num . " " . $street1;
		$my_post_title = $new_title;
	endif;
return $my_post_title;
}

add_filter('name_save_pre', 'save_name');
function save_name($my_post_name) {
	if ($_POST['post_type'] == 'listings') :
		$street1 = $_POST['wpcf']['street-1'];
		$house_num = $_POST['wpcf']['house-number'];
		$new_name =  $house_num . " " . $street1;
		$my_post_name = $new_name;
	endif;
return $my_post_name;
}

Thanks again, mate.
Eric

Le sujet ‘[Fermé] Custom fields as Post title’ est fermé à de nouvelles réponses.