query_post by title?

时间:2011-07-14 作者:v3nt

是否可以使用WP\\u Query或Query\\u posts创建帖子循环?

ie

$args = array(\'post_title\'=\'LIKE \'.$str.\'% \');

$res = WP_Query($arg);

// the loop...


// trying this now...
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like \'Abb%\' ");

echo count($mypostids).", ";    // works but can\'t echo out array of IDs for the next args?

$args = array(
    \'post__in\'=> $mypostids
);

$res = WP_Query($args);

while( $res->have_posts() ) : $res->the_post(); ...

5 个回复
最合适的回答,由SO网友:v3nt 整理而成

在这篇文章的帮助下,最终实现了这一目标。干杯,伙计们;

$finalArgs =  array (       
        \'posts_per_page\'=>5,
        \'order\' => \'ASC\',
        \'post_type\' => \'school\'                         
    );

    // Create a new instance
    $searchSchools = new WP_Query( $finalArgs );

    $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE \'".$str."%\' ");

    $args = array(
        \'post__in\'=> $mypostids,
        \'post_type\'=>\'school\',
        \'orderby\'=>\'title\',
        \'order\'=>\'asc\'
    );

    $res = new WP_Query($args);

    while( $res->have_posts() ) : $res->the_post();

        global $post;

        $EstablishmentNumber = get_post_meta($post->ID,\'EstablishmentNumber\', true);

        $schl = array(\'id\'=>$EstablishmentNumber, \'label\'=>$post->post_title , \'value\'=>$EstablishmentNumber );     
        $matchedSchools[] = $schl;


    endwhile;

SO网友:Scott

functions.php

<?php
add_filter( \'posts_where\', \'title_like_posts_where\', 10, 2 );
function title_like_posts_where( $where, $wp_query ) {
    global $wpdb;
    if ( $post_title_like = $wp_query->get( \'post_title_like\' ) ) {
        $where .= \' AND \' . $wpdb->posts . \'.post_title LIKE \\\'%\' . esc_sql( $wpdb->esc_like( $post_title_like ) ) . \'%\\\'\';
    }
    return $where;
}
?>

Then:

$args = array(
    \'post_title_like\' => $str
);
$res = new WP_Query($args);
SO网友:Zakir Sajib

从另一个循环获取标题

$title = get_the_title();
如果愿意,可以使用$title变量。

<?php

global $post, $current_post_id, $title;

function filter_where($where = \'\'){

    global $title;
    $where .= "AND post_title = \'$title\'";
    return $where;

}
add_filter(\'posts_where\', \'filter_where\');

$query = new WP_Query(array(\'post_type\' => \'sessions\') );
if ( have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

    /* Loop here */

endwhile; endif; 

wp_reset_query(); ?>

SO网友:Rajeev Vyas

是的,有可能。。。。

global $wpdb;

$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like \'%$str%\' ");

$args = array(\'post__in\' => $mypostids);

$res = WP_Query($arg);

SO网友:gtamborero

在我看来,这些回复似乎是在试图破解wordpress。

请参阅有关堆栈溢出的相同问题:

https://stackoverflow.com/questions/25761593/wp-query-with-post-title-like-something-and-category

如果要按标题进行搜索查询,请按标题排序:

$the_query = new WP_Query( 
  array(
    \'post_type\' => \'watches\',
    \'posts_per_page\' => 5,
    \'orderby\' => \'title\',
    \'s\' => \'my title\'
  ) 
);
此示例查询用于名为watches的帖子类型,“s”(搜索词)是您可以在查询中查找帖子标题的位置

结束

相关推荐

SANITIZE_TITLE_WITH_DASSES格式化函数是否过于自由(就接受的字符而言)?

sanitize_title_with_dashes (参考见下面的代码)是Wordpress用来格式化“漂亮”URL的函数。然而,与函数的注释头相反,它允许的不仅仅是字母数字字符、下划线(\\u0)和破折号(-)。它还允许诸如°、等标志。我如何才能真正做到只允许字母数字字符和破折号?/** * Sanitizes title, replacing whitespace with dashes. * * Limits the output to alphanumeric ch