Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按初始删除者自定义职位类型的自定义查询_Php_Wordpress - Fatal编程技术网

Php 按初始删除者自定义职位类型的自定义查询

Php 按初始删除者自定义职位类型的自定义查询,php,wordpress,Php,Wordpress,我想做的是根据标题的首字母列出一个自定义的帖子类型。我尝试了一些代码示例,但我似乎无法让它以我想要的方式完全运行。下面是我尝试过的一些代码,但没有成功 <div class="col-md-6"><!-- #### COLUMN #### --> <!-- ############################################## --> <!-- ##########

我想做的是根据标题的首字母列出一个自定义的帖子类型。我尝试了一些代码示例,但我似乎无法让它以我想要的方式完全运行。下面是我尝试过的一些代码,但没有成功

 <div class="col-md-6"><!-- #### COLUMN #### -->

                <!-- ############################################## -->
                <!-- ############### PAGE TITLE T ################# -->
                <!-- ############################################## -->

                <p class="f_header">PAGE TITLE T</p>

                <?php

                $args = array(

                    'post_type'             => 'movies',
                    'ignore_sticky_posts'   => TRUE,
                    'substring_where'       => 'T',

                    );

                    function restrict_by_first_letter( $where, $qry ) {
                      global $wpdb;
                      $sub = $qry->get('substring_where');
                      if (!empty($sub)) {
                        $where .= $wpdb->prepare(
                          " AND SUBSTRING( {$wpdb->movies}.movies_title, 1, 1 ) = %s ",
                          $sub
                        );
                      }
                      return $where;
                    }
                    add_filter( 'movies_where' , 'restrict_by_first_letter', 1 , 2 );

                    $results = new WP_Query( $args );

                ?>

                <table width="100%"><!-- #### TABLE #### -->

                    <?php

                        var_dump($results->request);

                        var_dump(wp_list_pluck($results->movies,'movies_title'));

                    ?>

                    <tr style="border-bottom: thin solid #111;">

                        <td class="movie_genre">    

                            <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>

                        </td>

                    </tr>

                </table><!-- #### / TABLE #### -->

                <?php wp_reset_postdata(); ?>

            </div><!-- #### / COLUMN #### -->

页面标题T


在我看来,您应该使用
posts\u where
过滤器,而不是指定的
movies\u where
。看起来您正在调用不存在的数据库数据。您需要在
$wpdb->posts
表中调用
post\u title

我会尝试如下方式更新您的代码:

function restrict_by_first_letter( $where, $qry ) {
    global $wpdb;
    $sub = $qry->get('substring_where');
    if (!empty($sub)) {
        $where .= $wpdb->prepare(
        " AND SUBSTRING( {$wpdb->posts}.post_title, 1, 1 ) = %s ", $sub
        );
    }
    return $where;
}
add_filter( 'posts_where' , 'restrict_by_first_letter', 1 , 2 );

我也会将这段代码(我复制到这个答案中的代码部分)移动到
functions.php

谢谢你的输入,但我按照你说的做了,我得到了这个结果..string(264)“从wp_posts中选择SQL_CALC_FOUND_行wp_posts.ID,其中1=1,wp_posts.post_type='movies'和(wp_posts.post_status='publish'或wp_posts.post_status='private')和子字符串(.movies_title,1,1)='T'顺序由wp_posts.post_date DESC LIMIT 0,10“数组(0){}title tI更新了我的答案。看起来你调用了一些不存在的数据库数据。好的,我已经更新了代码,现在我让它以超链接的形式回显标题,但它只是回显单词数组,但超链接有效:/,…计算出了标题。。