wp_query to show pages that starts with some character

允我心安 提交于 2019-12-10 21:45:28

问题


have question about wp-query function in wordpress. My question is, is it possible to show just pages that starts with some char. For example I want show all pages that starts with char A and also that page is child of some parent page. I know how to show child page with some full name:

$query = new WP_Query( 'pagename=contact_us/canada' );

Now I want something like:

$query = new WP_Query( 'pagename=contact_us/c%' ); 

So all pages that name begins with c ? I try this with with SQL logic and use % for any char. But it is not work for me.


回答1:


I figure out this and if someone will have same issue here is the solution:

<?php
    $child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_title like 'T%' AND post_parent = '746' AND post_status='publish'");    ?>
    <?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
    <div class="leksikon_rezultat">

            <span><?php echo $pageChild->post_title; ?></span>
            <a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo "Opširnije"; ?>"><?php echo "Opširnije"; ?></a>
    </div>
    <?php endforeach; endif;

So with $wpdb (object of wordpress class wpdb) you can use classic SQL to get any data from any table in wordpress database.



来源:https://stackoverflow.com/questions/13712272/wp-query-to-show-pages-that-starts-with-some-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!