Php Wordpress从标题(任何地方)获取结果,而不仅仅是第一个单词
目前,这是作为standalonesearch.php查询的工作代码 我有一个问题,它搜索标题的开头,而不是标题中我真正需要的任何地方 我有一种感觉,我已经接近解决方案了,我尝试了几件事情,这可能是我在post$args中缺少的东西Php Wordpress从标题(任何地方)获取结果,而不仅仅是第一个单词,php,arrays,ajax,wordpress,wordpress-theming,Php,Arrays,Ajax,Wordpress,Wordpress Theming,目前,这是作为standalonesearch.php查询的工作代码 我有一个问题,它搜索标题的开头,而不是标题中我真正需要的任何地方 我有一种感觉,我已经接近解决方案了,我尝试了几件事情,这可能是我在post$args中缺少的东西 global $wp_query; $type = 'qa'; $args=array( 'post_type' => $type, 'post_status' => 'publish', 'showposts'=>100,
global $wp_query;
$type = 'qa';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'showposts'=>100,
'orderby'=> 'menu_order',
'order' => 'DESC'
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$title = get_the_title();
$url = get_the_permalink();
$data[$title] = $url;
endwhile;
}
if(isset($_POST['latestQuery'])){
$latestQuery = $_POST['latestQuery'];
$latestQueryLength = strlen($latestQuery);
$result = array();
foreach($data as $name => $url){
if (substr(strtolower($name),0,$latestQueryLength) == strtolower($latestQuery)){
$result[$name] = $url;
}
}
echo json_encode($result);
}
这已经在本地环境中起作用
JavaScript queries search.php文件(如上),它在其中获取基于查询返回的json数组。。。例如“cla…”
所以,我已经适当地重新调整了两个项目,只有“经典”中提到的职位标题
如果我输入“som”或“els”,它不会返回任何内容,并且应该匹配所有“some”和“else”项
我认为这不是javascript代码的问题,而是PHP代码的问题
谢谢 解决了将来是否对某人有帮助的问题
if(isset($_POST['latestQuery'])){
$latestQuery = $_POST['latestQuery'];
global $wp_query;
$type = 'qa';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
's' => "$latestQuery", // defined query
'showposts'=>100,
'orderby'=> 'menu_order',
'order' => 'DESC',
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$title = get_the_title();
$url = get_the_permalink();
$data[$title] = $url;
endwhile;
}
$result = array();
foreach($data as $name => $url){ // removed if
$result[$name] = $url;
}
echo json_encode($result);
}
您正在尝试创建自定义搜索吗?您好ThemesCreator。我刚刚更新了主帖子,因为我无法格式化评论。
if(isset($_POST['latestQuery'])){
$latestQuery = $_POST['latestQuery'];
global $wp_query;
$type = 'qa';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
's' => "$latestQuery", // defined query
'showposts'=>100,
'orderby'=> 'menu_order',
'order' => 'DESC',
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$title = get_the_title();
$url = get_the_permalink();
$data[$title] = $url;
endwhile;
}
$result = array();
foreach($data as $name => $url){ // removed if
$result[$name] = $url;
}
echo json_encode($result);
}