【发布时间】:2014-08-01 21:32:56
【问题描述】:
我有一个复杂的问题,希望我能充分解释。我们目前有一个包含数百个“激励”帖子的 wordpress 网站。这些“激励”帖子中的每一个都包含一个名为“激励 ID”的自定义字段。我想根据来自单独表中的 PHP/MySQL 查询的“激励 ID”列表显示“推荐激励”页面。我可以像这样在自定义页面模板中使用 wp_query,通过自定义字段“激励 ID”创建帖子列表
<?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(). ?>
但是,每当我尝试将 PHP/MySQL 查询中的数组插入“meta_value”字段时,我得到的结果好坏参半。有时页面是空白的,有时我收到一个数组,但它会覆盖整个页面。任何人都可以让我深入了解如何正确使用 wp_query 根据自定义字段“激励 ID”值数组填充帖子列表?这是我当前的所有代码
<?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();
任何帮助将不胜感激!
【问题讨论】:
标签: php mysql arrays wordpress