自定义MySQL查询以提取高级自定义字段?

时间:2016-05-18 作者:Osu

我正在尝试修改My Wish List plugin 以便检索一些自定义字段(我使用Advanced Custom Fields plugin 在管理端创建自定义字段)。

该插件使用下面的自定义查询提取特定数据,但我想在该循环中检索每个帖子的自定义字段。谈到MySQL,我非常不懂,但我想我需要在SELECT 这样的查询也可以检索自定义字段,对吗?:

b.post_meta AS posts_meta,

Here\'s the plugin\'s MySQL query in full:

$posts = $wpdb->get_results(
    $wpdb->prepare(
        \'SELECT
                b.ID AS posts_id,
                b.post_title AS posts_title,
                b.post_content AS posts_content,
                b.post_date AS posts_date,
                c.ID AS authors_id,
                c.display_name AS authors_name
            FROM \'.$var_setting[\'table\'].\' a
            LEFT JOIN \'.$var_setting[\'table_posts\'].\' b
            ON a.item_id = b.ID
            LEFT JOIN \'.$var_setting[\'table_users\']." c
            ON c.ID = b.post_author
            WHERE
                b.post_status = \'publish\'
                AND a.user_id = %s
            ORDER BY b.post_title DESC",
            $user_id
    )
);

And here\'s the foreach loop that the plugin uses to pull in the content to display:

if ($posts != null) {
        if ($share_list == \'yes\') {
            $html = \'\';
            $html = file_get_contents($templates_html[\'box_list_share\'].$locale);
            $permalink = get_permalink();
            if (strpos($permalink, \'?\') !== false) {
                $html = str_replace(\'##pageID##\', $permalink.\'&\', $html);
            } else {
                $html = str_replace(\'##pageID##\', $permalink.\'?\', $html);
            }
            $html = str_replace(\'##userID##\', $user_id, $html);

            echo($html);
        }

        if ($show_count == \'yes\') {
            $html = \'\';
            $html = file_get_contents($templates_html[\'box_list_count\'].$locale);
            $count = $wpdb->num_rows;
            $html = str_replace(\'##count##\', $count, $html);

            echo($html);
        }

        foreach ($posts as $post) {
            $postId = $post->posts_id;
            $postDate = get_the_date(\'F j, Y\', $postId);
            $postAuthorId = $post->authors_id;
            $postAuthorName = $post->authors_name;
            $postContent = $post->posts_content;
            $postImage = wp_get_attachment_url(get_post_thumbnail_id($postId));
            $postTitle = $post->posts_title;
            $portTitleLang = extract_title($postTitle);
            $postUrl = get_permalink($postId);

            /* BEGIN : OSU EDIT */
            $postACF_sc =  get_post_meta($postId, \'sc_link\'); /* trying to get a specific custom field for each post */
            /* END : OSU EDIT */

            $args = array(
                \'styletarget\' => \'mylist\',
                \'item_id\' => $postId,
            );
            $html = \'\';
            $html = file_get_contents($templates_html[\'box_list\'].$locale);
            $html = str_replace(\'##postUrl##\', $postUrl, $html);

            /* BEGIN : OSU EDIT */
            $html = str_replace(\'##postACF_sc##\', $postACF_sc, $html); /* These str_replace() are how the plugin lets you add things like ##postUrl## using its templating system to display the permalink via AJAX */
            /* END : OSU EDIT */

            $html = str_replace(\'##postImage##\', $postImage, $html);
            if (strpos($postTitle, \'<!--:\') !== false || strpos($postTitle, \'[:\') !== false) { //means use mqtranlate or qtranlate-x
                    $html = str_replace(\'##postTitle##\', $portTitleLang[$lang], $html);
            } else {
                $html = str_replace(\'##postTitle##\', $postTitle, $html);
            }
            $html = str_replace(\'##postDate##\', $postDate, $html);
            $html = str_replace(\'##postAuthorName##\', $postAuthorName, $html);
            $html = str_replace(\'##postContent##\', $postContent, $html);
            $html = str_replace(\'##postBtn##\', gd_show_mylist_btn($args), $html);

            echo($html);
        }
    } else {
        $html = file_get_contents($templates_html[\'box_list_empty\'].$locale);
        echo($html);
    }

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

代码很好,我看不出有任何修改查询的需要-我想您丢失了the third argument in your get_post_meta call:

$postACF_sc =  get_post_meta($postId, \'sc_link\', true /* $single */);
您可能还想在foreach 要保存多个数据库查询,请执行以下操作:

update_postmeta_cache( wp_list_pluck( $posts, \'posts_id\' ) );

SO网友:Michael

要获取帖子的自定义字段,您需要添加以下内容:

d.meta_value (in the select clause)

LEFT JOIN \'.$var_setting[\'table_postmeta\'].\' d
        ON d.post_id = b.ID (in the from clause)
我不知道这个插件,所以我不能保证它会工作,但这就是加入Posteta表以提取自定义字段的方式。

希望这有帮助。