【问题标题】:How to display Wordpress posts from an array of custom field values如何从一组自定义字段值中显示 Wordpress 帖子
【发布时间】:2014-08-02 05:09:18
【问题描述】:

我们的网站包含数百个自定义“奖励”帖子。这些帖子中的每一个都包含一个名为“激励 ID”的自定义字段,它是一个数字。然后,我们对“推荐激励”表执行 PHP/MySQL 查询,该表返回逗号分隔的“激励 ID”列表。我们希望根据激励 ID 的逗号分隔列表显示“激励”帖子列表。我已经创建了一个 PHP 短代码脚本,用于在此处查询逗号分隔列表

<?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']. ",";
}

?>

我还创建了一个自定义页面模板,该模板使用 wp_query 填充 Wordpress“激励”帖子。当我有“激励 ID”的静态值时,此页面模板效果很好。

$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(). ?>

但是,我在尝试将逗号分隔的“激励 ID”列表插入 wp_query args“meta_value”字段时遇到了很多麻烦。我已经尝试了我能想到的一切,但我尝试的一切都没有。这个我试过了

$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'
);

这个

$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'
);

这个

$args = array(

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

但是这些选项都没有奏效。它们要么导致致命错误,要么导致空白页。我究竟做错了什么!?!?!?!任何人都可以给我的任何帮助将不胜感激!!!

【问题讨论】:

    标签: php mysql arrays wordpress


    【解决方案1】:

    您需要提供一个 Incentices ID 数组而不是字符串。

    当有多个值时,您应该使用meta_query 来获取带有自定义字段的帖子。

    $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'
            )
        )
    );
    

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 2013-03-09
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多