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!