2

I have an ajax function which switches the view type of the user's posts from list mode to grid mode via ajax but the problem is that when the wp query is being called it can't get the id of the user's profile that is being viewed. It works when I manually input an id number but it doesn''t work when I try to use things like $user->ID or $user_id or $user['ID']. Can someone please help me figure this out?

Here is my current php function and it's written inside my functions.php file:

function profile_view(){

$args = array(
        'post_type' => 'listings',
        'post_status'    => 'publish',
        'meta_query' => array(
            array(
                'key' => 'stm_car_user',
                'value' => $user->ID
            )
        )
    );

$query = new WP_Query($args);
//$query = stm_user_listings_query('1', 'publish');

$response = array();

//Grid/list settings
$view_list = '';
$view_grid = '';
$view_map = '';
$current_link_args = array();
if(!empty($_GET)){
    $current_link_args = $_GET;
}

$view_list_link = $view_grid_link = $view_map_link = $current_link_args;
$view_list_link['view_type'] = 'list';
$view_grid_link['view_type'] = 'grid';
$view_map_link['view_type'] = 'map';


if(!empty($_GET['view_type'])) {
    if ( $_GET['view_type'] == 'list' ) {
        $view_list = 'active';
    } elseif ( $_GET['view_type'] == 'grid' ) {
        $view_grid = 'active';
        $current_link_args['view_type'] = 'grid';
    }
    elseif ( $_GET['view_type'] == 'map' ) {
        $view_map = 'active';
        $current_link_args['view_type'] = 'map';
    }
} else {
    $view_list = 'active';
}


    if ( $query->have_posts() ):
    ob_start();

    $template = 'partials/listing-cars/listing-list-directory-loop';
    if(!empty($_GET['view_type']) and $_GET['view_type'] == 'grid') {

    $template = 'partials/listing-cars/listing-grid-directory-loop';

    } elseif(!empty($_GET['view_type']) and $_GET['view_type'] == 'map') {

    $template = 'partials/listing-cars/listing-map';

    } else {

    $template = 'partials/listing-cars/listing-list-directory-loop';

    }


    while ( $query->have_posts() ) {
        $query->the_post();

        get_template_part($template);

    }

    $response['html'] = ob_get_contents();
    ob_end_clean();
else:

endif;
wp_reset_postdata();

$show_pagination = true;
if(!empty($query->found_posts) and !empty($query->query_vars['posts_per_page'])) {
    if($query->found_posts < $query->query_vars['posts_per_page']) {
        $show_pagination = false;
    }
}

$response = json_encode( $response );

echo $response;
exit;
}

 add_action('wp_ajax_profile_view' , 'profile_view');
 add_action('wp_ajax_nopriv_profile_view','profile_view');

I'm not looking to use wp_get_current_user() either as that only shows posts from currently logged in users, I want to be able to see all posts by a certain user when I'm viewing their profile even when I'm not logged in...

Here is the ajax function:

$('.stm-view-by-profile a:not(.view-map)').on('click', function(e){
        e.preventDefault();
        var viewType = $(this).data('view');
        $('#stm_view_type_profile').val(viewType);
        $('.stm-view-by-profile a').removeClass('active');
        $(this).addClass('active');

        var data_form = $('.user_posts').serialize();

        $.ajax({
            url: ajaxurl,
            dataType: 'json',
            context: this,
            data: data_form + '&action=profile_view',
            beforeSend: function(){
                $('.stm-ajax-row').addClass('stm-loading');
            },
            success: function (data) {
            console.log(data);
           $('.stm-isotope-sorting').html(data.html);
           $('.stm-ajax-row').removeClass('stm-loading');
            }
        });
    });
2
  • viewing what profile, where?
    – Milo
    Commented May 18, 2018 at 15:31
  • @Milo say for example I'm on www.mydomain.com/author/someauthor and there is a list of posts by that particular author and it's in list mode, I want to switch to grid mode on the click of a button by ajax and the above code works and switches it to grid mode but it doesn't display the correct posts...
    – Matt H
    Commented May 18, 2018 at 15:36

1 Answer 1

1

EDIT: You need to get the variable from AJAX in you case you need to do:

function profile_view() {
$user_id = $_GET["user_id"]; // name of hidden input

$args = array(
        'post_type' => 'listings',
        'post_status'    => 'publish',
        'meta_query' => array(
            array(
                'key' => 'stm_car_user',
                'value' => $user_id
            )
        )
    );
...
8
  • That only shows posts from currently logged in users I don' want that, I want to be able to see all posts by a certain user when I'm viewing their profile even when I'm not logged in...
    – Matt H
    Commented May 18, 2018 at 15:28
  • So, you need to pass a variable from JS to php with the user id?
    – IvanMunoz
    Commented May 18, 2018 at 15:33
  • Yes I just need the user_id to display or be fetched or be called or whatever...I can't find the correct method of doing so...
    – Matt H
    Commented May 18, 2018 at 15:35
  • How do you call wp_ajax_profile_view? jQuery? And when do you call it?
    – IvanMunoz
    Commented May 18, 2018 at 15:37
  • I will edit my question and add it in and it's called when the button is clicked.
    – Matt H
    Commented May 18, 2018 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.