0

I'm trying to modify the My Wish List plugin so that I can retrieve some custom fields (I'm using the Advanced Custom Fields plugin to create my custom fields in the admin end).

The plugin uses the custom query below to pull out specific data, but I want to retrieve each post's custom fields as well in that loop. I'm pretty clueless when it comes to MySQL, but I'm assuming I need to add a line to SELECT like this to the query to be able to retrieve custom fields as well right?:

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);
    }
5
  • Are you changing the My Wish List plugin file(s), or are you taking a copy of its code and using it in your own theme/plugin? Commented May 18, 2016 at 9:28
  • I'm literally editing the plugin file directly. I know it's bad practice, but hopefully it will be a simple edit to do and I'm leaving comments where I've edited the files. Will do my best to integrate my changes into updates to the plugin!
    – Osu
    Commented May 18, 2016 at 9:34
  • We would need to see more code i.e. what the plugin subsequently does with $posts Commented May 18, 2016 at 9:37
  • Ok, I'll update the original post to show the foreach loop that is used to display content, one sec...
    – Osu
    Commented May 18, 2016 at 9:37
  • That's updated now - I've added comments with 'OSU EDIT' so you can see what I'm trying to do and where I've modified the plugin code
    – Osu
    Commented May 18, 2016 at 9:44

2 Answers 2

0

The code is fine, I can't see any need to modify the query - I think you're missing the third argument in your get_post_meta call:

$postACF_sc =  get_post_meta($postId, 'sc_link', true /* $single */);

You might also want to add this snippet just before the foreach to save multiple database queries:

update_postmeta_cache( wp_list_pluck( $posts, 'posts_id' ) );
1
  • Perfect! Looks like the third argument sorted it - thanks so much!
    – Osu
    Commented May 18, 2016 at 10:40
0

To get the custom fields for a post you would need to add something like the following:

d.meta_value (in the select clause)

LEFT JOIN '.$var_setting['table_postmeta'].' d
        ON d.post_id = b.ID (in the from clause)

I don't know the plugin so I can't promise it will work but this is how you join to the postmeta table to pull the custom fields.

Hope this helps.

3
  • Thanks for this, so should it look like this? gist.github.com/osuosu/e4f4a722f9652e51976b0e337f8302f2
    – Osu
    Commented May 18, 2016 at 9:55
  • Looks good. Give it a try :-)
    – Michael
    Commented May 18, 2016 at 10:09
  • Unfortunately, it breaks the code... but really appreciate your help! Looks like I got things sorted with TheDeadMedic's suggestion :)
    – Osu
    Commented May 18, 2016 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.