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
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
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
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