【问题标题】:Wordpress WP_Query/get_posts where post_title equals returns all resultsWordpress WP_Query/get_posts where post_title equals 返回所有结果
【发布时间】:2015-08-12 08:43:23
【问题描述】:

我创建了一个名为“sn-ps”的自定义帖子类型,其中包含客户端可以更改的“地址”等数据的 sn-ps。

sn-ps 的标题都是唯一的:

我创建了一个简单的函数来返回这些 sn-ps,但是它工作不正常,我不知道为什么。

功能:

function get_snippet($snippet_title) {
    $snippet_page = new WP_Query(array(
        'post_type' => 'snippets',
        'post_title' => $snippet_title
    ));
    return $snippet_page->posts[0]->post_content;
}

这是一个调用函数的示例:

echo get_snippet('footer_address');

问题是:

它总是会返回所有的 sn-ps,而不是按 post_title 过滤。

即使我使用get_posts(),它总是会以相同的顺序返回所有sn-ps,并且不会根据post_title返回单个sn-p。

结果相当于:

$snippet_page = new WP_Query(array(
    'post_type' => 'snippets'
));

为什么不按'post_title'过滤?

谢谢

(我也在使用多站点)

这是我的自定义帖子类型初始化代码:

function snippets() {
    $labels = array(
        'name'                => _x( 'Snippets', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Snippet', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Snippets', 'text_domain' ),
        'name_admin_bar'      => __( 'Snippets', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
        'all_items'           => __( 'All Snippets', 'text_domain' ),
        'add_new_item'        => __( 'Add New Snippet', 'text_domain' ),
        'add_new'             => __( 'Add New Snippet', 'text_domain' ),
        'new_item'            => __( 'New Snippet', 'text_domain' ),
        'edit_item'           => __( 'Edit Snippet', 'text_domain' ),
        'update_item'         => __( 'Update Snippet', 'text_domain' ),
        'view_item'           => __( 'View Snippet', 'text_domain' ),
        'search_items'        => __( 'Search Snippet', 'text_domain' ),
        'not_found'           => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'snippets', 'text_domain' ),
        'description'         => __( 'For custom snippets of code/data.', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( ),
        'hierarchical'        => false,
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_icon'           => 'dashicons-tagcloud',
        'menu_position'       => 5,
        'show_in_admin_bar'   => true,
        'show_in_nav_menus'   => false,
        'can_export'          => true,
        'has_archive'         => true,      
        'exclude_from_search' => true,
        'publicly_queryable'  => false,
        'capability_type'     => 'page',
    );
    register_post_type( 'snippets', $args );
}
add_action( 'init', 'snippets', 0 );

【问题讨论】:

  • 通过标题查询帖子见wordpress.stackexchange.com/a/18715/62744
  • 好吧,只需将 LIKE 替换为 = 即可。据我所知并考虑到WP_Query documentation,您似乎无法通过标题进行查询,但您可以通过 slug 名称进行查询。
  • 在这种情况下,如果您无法使用post_title 进行查询,我将不会得到更好的答案!感谢您的回复,测试和工作:)

标签: php wordpress


【解决方案1】:

我知道这是一篇很老的帖子,但也许有人会像我今天一样寻找解决方案。

原来你只需要使用'title'而不是'post_title'。

这是一个例子:

$author_id = 20;
$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'post_status' => 'inherit',
    'title' => 'Reviewer Plugin - User review image',
    'author' => $author_id,
); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachedfile) {
        $mediaIds =  $attachedfile->ID .','. $mediaIds;
    }
    $mediaIds = rtrim($mediaIds,",");
} 
print_r($mediaIds);

【讨论】:

  • 很棒的发现!我已经更改了接受的答案(抱歉 Nim!)。
【解决方案2】:

如果还是找不到解决办法,不如试试下面手动查询:

global $wpdb; $res = $wpdb->get_results("SELECT * FROM wp_posts where post_type='snippet' AND post_title='".$snippet_title."'");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 2020-05-02
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多