Ralationship between custom post type and users

This topic contains 17 reply and 2 voices, and was last updated by noomia 11 years, 7 months ago
Viewing 15 Posts - 1 through 15 (of 17 total)
Author Posts
April 22, 2013 at 2:10 pm 2704
noomia

Hi !

First of all, thank you for your great plugin ! I work with it for 1week and it's incredible what it allows me to do ! Thanks !

First of all, I have a custom post types wich is companies. It is a listing of the web agencies in my country. What I need is to “link” users to companies.

So I was wondering if we can create a select box with all the items of a custom post type on the profile page. Like the plugin “Advanced  Custom Fields” does with his “RelationShip” field. How could I do that right now ? I don't want to store the company name, but the company ID, it's more safe :) ! I think it could be great to add this "Relationship" feature in a future update, it's very useful :) !

Thank you very much,

Noomia

April 22, 2013 at 8:19 pm 2733
Tareq Hasan Tareq Hasan

Thanks for tip, I am adding this to the todo list. But no ETA, you can implement this currently with the action hook field 🙂

April 23, 2013 at 12:01 pm 2770
noomia noomia

Hi Tareq,

 

Unfortunately, I don’t understand how to deal with the action hook field to achieve my goal…

April 23, 2013 at 8:57 pm 2796
Tareq Hasan Tareq Hasan

With the action hook field, you can render your own input area or select area. Lets say you’ve a action hook named `my_own_hook`, you can generate your own fields like this:

[php]
function render_my_own_dropdown( $form_id, $post_id, $form_settings ) {
// generate your post dropdown
}

add_action( ‘my_own_hook’, ‘render_my_own_dropdown’, 10, 3 );
[/php]

Now as you’ve generated the dropdown on your own, when hit the submit button, save them:

[php]
function save_my_own_dropdown( $post_id ) {
// save your dropdown to post meta
}

add_action( ‘wpuf_add_post_after_insert’, ‘save_my_own_dropdown’ );
[/php]

April 24, 2013 at 9:09 am 2807
noomia noomia

Hi Tareq,

Here is what I’ve done :

`
/**
* Action Hook Companies dropdown
*/
function render_my_companies_dropdown( $form_id, $post_id, $form_settings ) {
// generate your post dropdown
$myCompany = get_post_meta( $post_id, ‘myCompany’, true ); ?>
‘companies’,
‘showposts’ => -1,
‘post_status’ => ‘publish’,
‘order’ => ‘ASC’,
‘orderby’ => ‘title’
) ); ?>

April 24, 2013 at 9:45 am 2809
Tareq Hasan Tareq Hasan

The code should be working, the only problem I see in the code is query_posts() function, you should never use that instead of special cases. Use WP_Query and try again.

April 24, 2013 at 9:51 am 2810
noomia noomia

I’ve forgot to mention that it’s not a post meta, but a user meta. And what if the meta doesn’t exit ? It has to be created…

I’ll try wuth WP_Query, but I don’t think that is the problem cause I have all my companies in the dropdown…

April 24, 2013 at 9:59 am 2812
Tareq Hasan Tareq Hasan

If it’s user meta, then use update_user_meta(), it’ll be created if it doesn’t exists.

April 24, 2013 at 11:21 am 2822
noomia noomia

I’ve done what you say, I can’t do it…

`
/**
* Action Hook Companies dropdown
*/
function render_my_companies_dropdown( $form_id, $post_id, $form_settings ) {
// generate your post dropdown
$myCompany = get_user_meta( $post_id, ‘myCompany’, true );

$args= array(
‘post_type’ => ‘companies’,
‘showposts’ => -1,
‘post_status’ => ‘publish’,
‘order’ => ‘ASC’,
‘orderby’ => ‘title’
);

$my_companies_query = new WP_Query($args);

?>

April 24, 2013 at 11:24 am 2826
Tareq Hasan Tareq Hasan

Why are you updating the user meta with the post ID? Shouldn’t it be the user id?

April 24, 2013 at 11:32 am 2827
noomia noomia

I have changed the
`get_user_meta( $post_id, ‘myCompany’, true );`
to
`get_user_meta( get_current_user_id(), ‘myCompany’, true );`

and
`update_user_meta( $post_id, ‘myCompany’, $_POST[‘myCompany’] );`
to
`update_user_meta( get_current_user_id(), ‘myCompany’, $_POST[‘myCompany’] );`

But nothing changes…

Here is the full code :
`
/**
* Action Hook Companies dropdown
*/
function render_my_companies_dropdown( $form_id, $post_id, $form_settings ) {
// generate your post dropdown
$myCompany = get_user_meta( get_current_user_id(), ‘myCompany’, true );
var_dump($myCompany);
var_dump(get_current_user_id());
$args= array(
‘post_type’ => ‘companies’,
‘showposts’ => -1,
‘post_status’ => ‘publish’,
‘order’ => ‘ASC’,
‘orderby’ => ‘title’
);

$my_companies_query = new WP_Query($args);

?>

April 24, 2013 at 11:34 am 2828
noomia noomia

I feel that it doesn’t go through the wpuf_add_post_after_insert… I mention abose that it’s when I edit user profile… Do I have to change something ?

April 24, 2013 at 12:32 pm 2833
noomia noomia

After further investigation, I’ve found `wpuf_update_profile_resp`

So I use it like that, instead of
`
function save_my_companies_dropdown( $post_id ) {
// save your dropdown to post meta
update_user_meta( get_current_user_id(), ‘myCompany’, $_POST[‘myCompany’] );
}
add_action( ‘wpuf_add_post_after_insert’, ‘save_my_companies_dropdown’ );
`
I do that
`
function save_my_companies_dropdown( $response, $user_id, $form_id, $form_settings ) {
// save your dropdown to post meta
update_user_meta( get_current_user_id(), ‘myCompany’, $_POST[‘myCompany’] );
return($response);
}
add_action( ‘wpuf_update_profile_resp’, ‘save_my_companies_dropdown’ );
`
Is that right ? Do I have to return the response ?
Thanks

April 24, 2013 at 8:41 pm 2840
Tareq Hasan Tareq Hasan

No, you shouldn’t modify the response.

I tested your code from above post, it perfectly saved the post id to that user profile.

April 24, 2013 at 9:02 pm 2843
noomia noomia

it’s weird :/ ! With update_profile_resp, it works… But with the other code it doesn’t :/ ! Could you maybe have look on my dev website ?

April 24, 2013 at 9:08 pm 2844
Tareq Hasan Tareq Hasan

I thought you were adding the field in post form? Are you adding this on edit profile? Then the `wpuf_add_post_after_insert` won’t work. You need to use `wpuf_update_profile` hook.

Viewing 15 Posts - 1 through 15 (of 17 total)