dsf5989 2016-11-29 15:37
浏览 46
已采纳

WordPress返回数组,其帖子ID基于meta_value

I'm trying to figure out something that should be fairly easy I think. I have custom fields named 'include-in-nav'. I'm using wp_nav_menu() to create a menu:

wp_nav_menu( array(     'theme_location' => 'primary', 
                        'menu_id' => 'primary-menu',
                        'include' => $post_ids

                ) 
        ); 

I've tried to get an array with post id's based on wether 'include-in-nav' is true or not. I've tried get_posts() and WP_query() but whatever i'm trying, nothing seems to return an array with post ID's. Any suggestions?

-edit my attempt to get $post_ids:

$post_ids = get_posts(array(
        'post'
        'meta_value'    => 'include-in-main-nav'
));

and

$nav_posts = array(
        'meta_value'    => 'include-in-main-nav'
));

$wp_query = new WP_Query($nav_posts);

$post_ids = array();

while ( $wp_query->have_posts() ) : $wp_query->the_post();
   $post_ids[] = get_the_ID() ;
endwhile;
  • 写回答

1条回答 默认 最新

  • dongzhanlu0658 2016-11-29 15:46
    关注

    well other than a flat our sql query, nope.

    you can use the wp_query with a meta query nested in. this will return posts though. you will need to loop through the posts as you would normally

    example of a query like the one you need:

    $args = array(
    'post_type'  => 'page',
    'meta_key'   => 'include-in-main-nav',
    'meta_query' => array(
        array(
            'key'     => 'include-in-main-nav',
            'value'   => 1,
            'compare' => '=',
        ),
    ),
    );
     $query = new WP_Query( $args );
    
    
    if ( $query->have_posts() ) : ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>    
            <!-- MOO! time to display your menu items! -->
        <?php endwhile; ?>
    <?php endif; ?>
    

    note: from there you can also fill an array with your ids.. if you really really need an array with only your ids in it.

    Have fun! hope this helps you out m8!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?