Home Forums Plugin Support WP User Frontend Pro Ralationship between custom post type and users

This topic is: resolved

This topic contains 17 replies, has 2 voices, and was last updated by  noomia 5 years, 9 months ago.

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #2704

    noomia
    Participant
    Post count: 115

    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

    #2733

    Tareq Hasan
    Keymaster
    Post count: 2831

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

    #2770

    noomia
    Participant
    Post count: 115

    Hi Tareq,

     

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

    #2796

    Tareq Hasan
    Keymaster
    Post count: 2831

    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]

    #2807

    noomia
    Participant
    Post count: 115

    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 ); ?>
    < ?php query_posts(array( 'post_type' => ‘companies',
    ‘showposts' => -1,
    ‘post_status' => ‘publish',
    ‘order' => ‘ASC',
    ‘orderby' => ‘title'
    ) ); ?>

    < ?php while ($my_companies_query->have_posts()) : $my_companies_query->the_post(); ?>

    #2826

    Tareq Hasan
    Keymaster
    Post count: 2831

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

    #2827

    noomia
    Participant
    Post count: 115

    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);

    ?>

  •  
  •