duanping1632 2014-06-11 17:55
浏览 59
已采纳

Wordpress - wp-query通过PHP / MySQL查询数组中的自定义字段列出帖子

I have a complicated question, hopefully I can explain it adequately. We currently have a wordpress site with hundreds of "Incentive" posts. Each of these "Incentive" posts contain a custom field titled, "Incentive ID". I want to display a page of "recommended incentives" based on a list of "Incentive ID's" that come from a PHP/MySQL query in a separate table. I can create a list of posts by the custom field "Incentive ID" by using wp_query in a custom page template like so

<?php 

// args
$args = array(

    'meta_key' => 'Incentive ID',
    'meta_value' => array(20,21),
    'orderby' => 'meta_value_num',
   'order' => 'ASC',
    'post_type' => 'incentives'
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
    <ul style="list-style:decimal;">
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

However, whenever I try to insert an array from my PHP/MySQL query into the 'meta_value" field, I get mixed results. Sometimes the page is blank, sometimes I receive an array but it overwrites the entire page. Can anyone give me some insight into how I can correctly use wp_query to populate a list of posts based on an array of custom field "Incentive ID" values? Here is all my current code

<?php
/**
 * Template Name: Recommended Incentives
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */
get_header(); ?>
<?php $con = mysql_connect("localhost","xxxxx","xxxxxx");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("lighting_incentives", $con);
$user_ID = get_current_user_id();
$result = mysql_query("SELECT FK_T_L_INCENTIVES_ID FROM WAYPOINT_USER_PICKED WHERE WP_RECOMMENDED = 1 AND FK_USER_ID = 31");

$rows = array();    
while($r = mysql_fetch_array($result)) {
    $row[0] = $r[0];
    array_push($rows,$row);
}
mysql_close($con);
?>
<div id="main-content" class="main-content"> 
    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
        <div class='entry-content' style='max-width:1130px; margin-left:372px; padding-right:0px;'><h3 style="text-align:center; font-size:34px; font-family:arial; font-weight:600;">Recommended Incentives</h3>
            <?php 
// args
$args = array(
    'meta_key' => 'Incentive ID',
    'meta_value' => print json_encode($rows, JSON_NUMERIC_CHECK),
    'orderby' => 'meta_value_num',
   'order' => 'ASC',
    'post_type' => 'incentives'
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
    <ul style="list-style:decimal;">
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

        </div> 
        </div><!-- #content -->
    </div><!-- #primary -->
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();

Any help would be much appreciated!

  • 写回答

2条回答 默认 最新

  • dqthn68688 2014-06-11 19:59
    关注

    First off when using WordPress don't use php mysql functions use $wpdb. Second you use a print json_encode that won't work. When you are trying to query by multiple meta values use the meta_query:

    // Fill this with the sql result
    $id_arrays = array();    
    while($r = mysql_fetch_array($result)) {
      $id_arrays[] = $r[0];
    }
    
    $args = array(
      'post_type' => 'incentives',
      'orderby' => 'meta_value_num',
      'order' => 'ASC',
      'meta_query' => array(
          array(
            'key' => 'Incentive ID',
            'value' => $id_arrays, // the first ID
            'compare' => 'IN'
         ),
       )
    );
    $query = new WP_Query( $args );
    

    Not sure if the orderby will work this way but it should. Also you see I'm hardcoding the first 2 entries. To make it dynamic loop over it before adding them to the array.

    For more information see:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题