1
votes

We have a website that contains hundreds of Custom "Incentives" posts. Each of these posts contain a custom field titled, "Incentive ID" which is a number. We then do a PHP/MySQL query to a "Recommended Incentives" table which returns a comma separated list of "Incentive ID's". We want to display a list of "Incentives" Posts based on the the comma separated list of Incentive ID's. I have already created a PHP shortcode script that queries the comma separated list here

<?php


global $wpdb;
$user_ID = get_current_user_id();
$sql = <<<SQL
SELECT FK_T_L_INCENTIVES_ID FROM lighting_incentives.WAYPOINT_USER_PICKED WHERE WP_RECOMMENDED = 1 AND FK_USER_ID = $user_ID
SQL;

if (!$sql) { // add this check.
    die('Invalid query: ' . mysql_error());
}

$resultset = array();
$rebates = $wpdb->get_results( $sql, ARRAY_A );
foreach($rebates as $data) {
     echo $data['FK_T_L_INCENTIVES_ID']. ",";
}

?>

I have also created a custom page template that populates Wordpress "Incentives" posts using wp_query. This page template works great when I have static values for the "Incentives ID's".

$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, I am having a lot of trouble trying to insert the comma separated list of "Incentives ID's" into the wp_query args "meta_value" field. I have tried everything I can think of but nothing I try works. I have tried this

$gg = print do_shortcode("[php snippet=35]");
$args = array(

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

This

$gg = print do_shortcode("[php snippet=35]");
$args = array(

    'meta_key' => 'Incentive ID',
    'meta_value' => $gg,
    'orderby' => 'meta_value_num',
   'order' => 'ASC',
    'post_type' => 'incentives'
);

This

$args = array(

    'meta_key' => 'Incentive ID',
    'meta_value' => print do_shortcode("[php snippet=35]"),
    'orderby' => 'meta_value_num',
   'order' => 'ASC',
    'post_type' => 'incentives'
);

But none of these options have worked. They either result in fatal errors or blank pages. What am I doing wrong!?!?!?! Any help that anyone can give me would be MUCH APPRECIATED!!!

1

1 Answers

4
votes

You need to provide an array of Incentices ID instead of a string.

You should use meta_query to get posts with custom fields when having multiple values.

$meta_values = array(value1, value2, /* .. */); // not a string

$args = array(
    'post_type' => 'incentives',
    'meta_key' => 'Incentive ID',
    'meta_value' => array(),
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'Incentive ID',
            'value' => $meta_values,
            'compare' => 'IN'
        )
    )
);