【问题标题】:Wordpress - wp-query to list posts by custom field from PHP/MySQL queried arrayWordpress - wp-query 按 PHP/MySQL 查询数组中的自定义字段列出帖子
【发布时间】: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


    【解决方案1】:

    首先在使用 WordPress 时不要使用 php mysql 函数使用 $wpdb。 其次,您使用不起作用的print json_encode。 当您尝试通过多个元值查询时,请使用 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 );
    

    不确定orderby 是否会以这种方式工作,但应该会。您还看到我正在对前 2 个条目进行硬编码。在将它们添加到数组之前使其动态循环。

    欲了解更多信息,请参阅:

    【讨论】:

    • 您好,感谢您的帮助!您能否详细说明如何为“meta_query”数组创建动态循环?我不确定这是怎么可能的
    • 查看我的更新。它添加了一种不同的方式来做这件事,有点短
    【解决方案2】:

    为了构建你$rows数组,你可以这样做

    while($r = mysql_fetch_array($result)) {
        $row[] = $r[0];
    }
    

    您可以使用meta_query 获取具有与其相关的特定元值的特定帖子。示例:

    $meta_query = array(
        array(
            'key' => 'Incentive ID',
            'value' => $row,
            'compare' => 'IN'
        )
    );
    
    $args = array( 
        'post_type' => "incentives",
        'post_status' => 'publish', 
        'meta_query' => $meta_query,
        'orderby' => 'meta_value_num', 
        'order' => 'ASC' 
        );
    
    // get results
    $the_query = new WP_Query( $args );
    

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 2021-06-29
      • 2012-07-04
      • 2012-04-29
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      相关资源
      最近更新 更多