Php WP查询和ACF字段
我不熟悉WP查询和ACF自定义字段。我想写一个代码,显示计算total_score自定义字段的前3个结果。我已经设法缩短了总分,但我想显示标题和前3篇文章的链接,以便访问者将点击并转到该文章。任何帮助都将不胜感激。到目前为止,我的代码是:Php WP查询和ACF字段,php,wordpress,Php,Wordpress,我不熟悉WP查询和ACF自定义字段。我想写一个代码,显示计算total_score自定义字段的前3个结果。我已经设法缩短了总分,但我想显示标题和前3篇文章的链接,以便访问者将点击并转到该文章。任何帮助都将不胜感激。到目前为止,我的代码是: $args = array( 'posts_per_page' => -1, 'post_title' => true,); $all_posts = array(); $the_query = new WP_Query( $args );
$args = array(
'posts_per_page' => -1,
'post_title' => true,);
$all_posts = array();
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ): $the_query->the_post();
// Get all fields
$fields = get_fields();
// Push each $fields array into the $all_posts array
array_push($all_posts, $fields);
endwhile;
// Restore original Post Data
wp_reset_postdata();
// Print the result here and do what you choose
print_r($all_posts);
endif;
if(isset($_POST['place']))
{ // start the loop
$q1=$_POST["place"];
//CORECT CODE !!!!
foreach($all_posts as &$value) {
if ($value['question1']==$q1){
$value['total_score']=$q1;
}
} } //end question 1
// question 2
if(isset($_POST['home']))
{ // start the loop
$q2=$_POST["home"];
foreach($all_posts as &$value) {
if ($value['question2']==$q2){
$value['total_score']=$value['total_score']+$q2;
}
//echo $value['total_score']."<br>"; }
//echo "Q2"."<br>";
//print_r($all_posts);
} //end question 2
// question 3
if(isset($_POST['hours']))
{ // start the loop
$q3=$_POST["hours"];
//CORECT CODE !!!!
foreach($all_posts as &$value) {
if ($value['question2']==$q3){
$value['total_score']=$value['total_score']+$q3;
}
}
//echo "Q2"."<br>";
} //end question 3
// shorting by total_score
function sortByOrder($a, $b) {
return $b['total_score'] - $a['total_score'];
}
usort($all_posts, 'sortByOrder');
//print_r($all_posts);
foreach($all_posts as &$value) {
echo $value['total_score']."<br>";
}
$args=数组(
“每页帖子数”=>-1,
“post_title”=>true,);
$all_posts=array();
$thew_query=newwp_query($args);
如果($the\u query->have\u posts()):
while($the_query->have_posts()):$the_query->the_post();
//获取所有字段
$fields=get_fields();
//将每个$fields数组推入$all_posts数组
数组\u推送($all\u post,$fields);
结束时;
//恢复原始Post数据
wp_reset_postdata();
//在此处打印结果并执行您选择的操作
打印(所有帖子);
endif;
如果(isset($_POST['place']))
{//启动循环
$q1=$_POST[“place”];
//正确的代码!!!!
foreach($all_posts as&$value){
如果($value['question1']=$q1){
$value['total_score']=$q1;
}
}}//结束问题1
//问题2
如果(isset($_POST['home']))
{//启动循环
$q2=$_POST[“home”];
foreach($all_posts as&$value){
如果($value['question2']=$q2){
$value['total_score']=$value['total_score']+$q2;
}
//echo$value['total_score']。“
”;}
//回声“Q2”。
”;
//打印(所有帖子);
}//结束问题2
//问题3
如果(isset($_POST['hours']))
{//启动循环
$q3=$_POST[“小时”];
//正确的代码!!!!
foreach($all_posts as&$value){
如果($value['question2']=$q3){
$value['total_score']=$value['total_score']+$q3;
}
}
//回声“Q2”。
”;
}//结束问题3
//按总分数做空
功能排序器($a,$b){
返回$b['total_score']-$a['total_score'];
}
usort($all_post,'sortByOrder');
//打印(所有帖子);
foreach($all_posts as&$value){
echo$value['total_score']。“
”;
}
您可以考虑使用$args
变量来限制和过滤WP\u查询的结果。设置“每页帖子”=>3
。这意味着查询中只返回3篇文章
但是,当您获取所有帖子并使用usort
对它们进行排序时,您可以将其替换为用于帮助您使用自定义字段进行查询。这将减少编译所需帖子所需的工作量
然后,您可以使用最终结果在3篇文章上循环并输出您的标题和永久链接。请仅用此代码替换您的代码。它只给你3个帖子和你的总分ASC或DESC 并根据注释替换中的数据 如果有任何疑问,请告诉我
<?php
$args = array(
'post_type' => 'portfolio', // your post type name
'orderby' => 'meta_value_num', // if total_score is string then use it - meta_value
'meta_key' => 'total_score', // your meta key name ( total_score custom field )
'posts_per_page' => 3,
'order' => 'ASC' // ASC or DESC
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
echo '<a href="'.get_the_permalink().'">'.get_the_title().'</a>';
endwhile;
endif;
wp_reset_postdata();
Glen谢谢你,但我不知道如何用你提到的资源交换代码。你能帮我为我要获取的结果添加一些.code吗?另外total_score字段是虚拟的。不保留任何值。我使用它在提交表单时为数组添加值,具体取决于表单下拉列表的值。很抱歉延迟。请参阅@Team Dolphin的答案非常感谢@Team Dolphin提供的代码。它似乎起作用了!我将在明天之前完成表格提交的所有总分并确认。你救了我几个小时的搜索!我有我的问题。在我的代码中,我想在提交时在total_score数组中添加$q1值。这是怎么实现的?实际上我有15个问题,我把它们存储在变量$q1、$q2、q3中。。e、 t.c.提交表格时,我想添加$q1、q2….的值。。。。在total_score数组中,然后获得3个排名靠前的total_score帖子的结果。我怎样才能得到这个结果?我不明白你的问题?如果我没有正确解释,很抱歉。我的页面是。在提交时,我想根据下拉选择添加一个值(总分)。这是一个匹配系统,因此当用户提交算法搜索并根据较高的分数(前3名)带来最佳匹配时。