Skip Navigation

[Closed] Can't order by meta_key in child posts

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Caridad 9 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#270630
Screen Shot 2014-12-18 at 16.42.01.png

Hi there,

I am trying to sort child post by custom field using the following code:

$childargs = array(
'post_type' => 'documentazione',
'numberposts' => -1,
'meta_key' => 'wpcf-doc_data',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_portfolio_id', 'value' => get_the_ID()))
);
$child_documentazione = get_posts($childargs);
    foreach ($child_documentazione as $documentazione) {
    $doc_autore = $documentazione->fields['doc_autore'];
    $doc_data = $documentazione->fields['doc_data'];
    $doc_titolo = $documentazione->fields['doc_titolo2'];
    $doc_pdf = $documentazione->fields['doc_pdf'];
    
    echo '<li>';
    echo $doc_autore . ', ' . $doc_data . '<strong>' . $doc_titolo . '</strong>';
    if(!empty($doc_pdf)) { 
    echo ', (<a href="' . $doc_pdf .'">PDF</a>)';
    }
    echo '</li>';
    }

But it return an empty list....

The second part of the code work like a charm, if I used with types_child_posts:

$child_documentazione = types_child_posts('documentazione', array("raw"=>"true","separator"=>"</li><li>"));
    foreach ($child_documentazione as $documentazione) {
    $doc_autore = $documentazione->fields['doc_autore'];
    $doc_data = $documentazione->fields['doc_data'];
    $doc_titolo = $documentazione->fields['doc_titolo2'];
    $doc_pdf = $documentazione->fields['doc_pdf'];
        
    echo '<li>';
    echo $doc_autore . ', ' . $doc_data . '<strong>' . $doc_titolo . '</strong>';
    if(!empty($doc_pdf)) { 
    echo ', (<a href="' . $doc_pdf .'">PDF</a>)';
    }
    echo '</li>';
    }

Thanks a lot
d

#270651

When you are sorting by a custom field value, there is side-effect.

Any posts that dont have this value wont come up.

This is a problem in WordPress WP_Query class and there is nothing we can do from here. The only solution for now is that you make sure that all your posts have this custom field created, even if its empty.

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,
Caridad

#270781

Dear Caridad,

thank you for your support. Unfortunately, I still need your assistance.

All the custom fields are properly filled with a value for each post, but the code still return an empty list.

Davide

#270940

The custom field you need to check is specifically "wpcf-doc_data".

The code looks correct, I would check if get_the_ID() is returning the correct ID.

Something you can test is to sort by a different field (ie: title) to confirm if the problem is here or not.

You can also try disabling WPML and other plugins that may affect the result.

I you can't find anything, can you share access details so I can take a look?

Thanks
Caridad

The topic ‘[Closed] Can't order by meta_key in child posts’ is closed to new replies.