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