$wpdb ajax-вызов для поиска и разбиения на страницы (пользовательский тип сообщения)Php

Кемеровские программисты php общаются здесь
Anonymous
$wpdb ajax-вызов для поиска и разбиения на страницы (пользовательский тип сообщения)

Сообщение Anonymous »

Я создал специальную публикацию «Новости» в Wordpress и хочу выполнять поиск сообщений и разбивать их на страницы без перезагрузки страницы.
Я использую класс $wpdb, и теперь он работает нормально, но когда я пытаюсь выполнить ajax-вызов, я не получаю никаких результатов.

Вот какая ошибка:

Код: Выделить всё

Notice[/b]:  Undefined variable: parent_slug in [b]    C:\xampp\htdocs\website\wp-content\themes\website\includes\news\output_more-news.php[/b] on line [b]14[/b][b]
Fatal error[/b]:  [b]    Call to undefined function get_terms() in C:\xampp\htdocs\website\wp-content\themes\website\includes\news\output_more-news.php[/b] on line [b]15[/b]
вот мой ajax-код:
jQuery( document ).ready(function() {

Код: Выделить всё

jQuery('#news_form').on('submit', function(e) {
var
that = jQuery(this),
url = "http://localhost/website/wp-content/themes/website/includes/news/output_more-news.php",
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index,value){
var
that = jQuery(this),
name = that.attr('name'),
value = that.val();
data[name]= value;
console.log(data);
});

jQuery.ajax({
url: url,
type: type,
data: data,
beforeSend : function (){
//do something like loading gif
},
success: function(data){
alert("form submited");
console.log(data);
},
error:function(){
alert("error");
}

});//ajax call

e.preventDefault()

});
вот выход_more-news.php

Код: Выделить всё

$search_term = "%" . strtolower($_POST['name_search'])."%";
if(!empty($_POST['archive-dropdown'])){
$archive_selected = $_POST['archive-dropdown'];
}else{
$archive_selected = "";
}
$taxonomy = ucfirst($parent_slug);
foreach(get_terms('news_categories') as $term){
if($term->slug == $parent_slug){
$term_ID = $term->term_id;
}
}
$home_url = get_home_url();
$site_directory_url = get_template_directory_uri();
$archive_selected = $_POST['archive-dropdown'];
$archive_selected = str_replace("$home_url","",$archive_selected );
$archive_selected = str_replace("?post_type=news","",$archive_selected );
$archive_date = preg_split("/[\/]+/",$archive_selected );
$archive_month = $archive_date[2];
$archive_year = $archive_date[1];

global $wpdb;

$more_news_query =
"
SELECT      $wpdb->posts.ID
FROM        $wpdb->posts
INNER JOIN  $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN  $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
AND         $wpdb->term_taxonomy.taxonomy = 'news_categories'
AND         $wpdb->term_taxonomy.term_id IN ($term_ID)
WHERE       $wpdb->posts.post_title LIKE '$search_term'
";
if(!empty($archive_selected)){
$more_news_query .=
"
AND         month($wpdb->posts.post_date) = '$archive_month'
AND         year($wpdb->posts.post_date) = '$archive_year'
";
}
$more_news_query .=
"
AND         $wpdb->posts.post_type = 'news'
AND         $wpdb->posts.post_status = 'publish'
ORDER BY    $wpdb->posts.post_date
";

$count_results = "SELECT COUNT(1) FROM (${more_news_query}) AS combined_table";
$total = $wpdb->get_var($count_results);
$items_per_page = get_option( 'posts_per_page' );
$paged = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $paged * $items_per_page ) - $items_per_page;
$max_num_pages = ceil($total / $items_per_page);
$more_news = $wpdb->get_results( $more_news_query .  " LIMIT ${offset}, ${items_per_page}" );
$prev_link = $paged - 1;
if($prev_link = $max_num_pages){
$nextlink = $max_num_pages;
}

echo"


[h4]
[url=#collapseMore]
More News
[/url]
[url=#collapseMore]
[i][/i]
[/url]
[/h4]
";
echo "";
echo "";
echo "";
echo "


News[i]
[/i]";
?>

 archives









Подробнее здесь: [url]https://stackoverflow.com/questions/38657334/wpdb-ajax-call-for-search-and-pagination-custom-post-type[/url]

Вернуться в «Php»