Assign user to custom taxonomy

Hello,

Is it possible to assign a user to a custom taxonomy using the CustomPress plugin?

Thank you,
Greg

  • gversion

    Hi Vinod,

    Thanks for your reply. I will explain in more detail what I am trying to achieve:

    I am using the WPMU Classifieds plugin with CustomPress. I am looking for a way to be able to filter Classifieds by advertisers' location (e.g. country). The location field should be associated with the user profile rather than with each classified (so the user does not need to manually select their location each time they post a classified).

    I was thinking if I could assign a user to a "Country" taxonomy with a value such as "United States" then I could easily list all classifieds from authors that are in the United States.

    Your advice and help is much appreciated.

    Thank you,
    Greg

  • Ash

    Hello @Greg

    I have registered at your site (atest), but I could not reach the add classified page. It always redirects to my classifieds page. Would you please share me a link?

    The location field should be associated with the user profile rather than with each classified

    I will give you some code so that the form location field is auto populated based on user profile country code.

    I was thinking if I could assign a user to a "Country" taxonomy with a value such as "United States" then I could easily list all classifieds from authors that are in the United States.

    That would not be possible I am afraid. What we can do is, to auto populate the form location field with the user's location field, so that user won't have to provide location each time he creates an ad. But to get posts for a certain location, you will need to use custom meta query. See here:
    http://wordpress.stackexchange.com/questions/144078/get-posts-by-meta-value
    http://stackoverflow.com/questions/11068795/wordpress-get-post-based-on-meta-field-content

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • gversion

    Hi Ash,

    Thanks so much for your help. It would be great if you can help get it working at the front-end and back-end!

    What happens if you hover over the "SELL" menu item and then select "Post a Classified"? That should load the Add Classified form. I have also set you as an Administrator so you have full access.

    By the way, can you please remove the screenshot from your previous post? I do not want any aspect of this site to be public.

    Thank you for your help.

    Regards,
    Greg

  • Ash

    Hello @Greg

    Please try the following code:

    add_action( 'wp_head', 'set_default_location' );
    function set_default_location() {
        $user_id = get_current_user_id();
        $key = "cc_country_code";
        $user_location = get_user_meta( $user_id, $key, true );
        ?>
        <script type="text/javascript">
        jQuery(function($){
            $('#listing_locations_checklist li').each(function() {
                var obj = $(this).find('label');
                if( $.trim(obj.text()) == '<?php echo $user_location ?>' ) {
                    obj.find('input').prop('checked', true);
                }
            });
        });
        </script>
        <?php
    }
    
    add_action( 'admin_head', 'set_default_location_admin' );
    function set_default_location_admin() {
        $user_id = get_current_user_id();
        $key = "cc_country_code";
        $user_location = get_user_meta( $user_id, $key, true );
        ?>
        <script type="text/javascript">
        jQuery(function($){
            $('#listing_locationschecklist li').each(function() {
                var obj = $(this).find('label');
                if( $.trim(obj.text()) == '<?php echo $user_location ?>' ) {
                    obj.find('input').prop('checked', true);
                }
            });
        });
        </script>
        <?php
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    In your admin, the above code may not work as I saw a js error in your admin end. You need to disable all other plugins and try the code.

    Note that, the code below add_action('admin_head' ........ ) is for admin end, above is for front end.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.