Hello,
I have an issue with a custom query:
This shortcode return 6:
//Shortcode para contar el número de CPT grupo que tienen 3 CPT contrataciones CHILD
function jm_grupo_tres_contrat (){
$grupos_ids = get_posts('post_type=grupo&posts_per_page=-1&fields=ids'); //Almacenamos en la variable $posts_ids todas las IDs de los grupos.
foreach( $grupos_ids as $grupo_ID ) { //recorremos todas las IDs almacenadas en $posts_ids, asignando en cada interacción un valor de ID a la variable $grupo_ID
$query3 = new WP_Query( //Post Relationships API de Toolset. Consulta los cpt contrataciones-1 que pertenecen al cpt grupo.
array(
'post_type' => 'contrataciones-1',
'post_status' => array('publish', 'pending'),
'meta_query' => array(
array(
'key' => '_wpcf_belongs_grupo_id',
'value' => $grupo_ID
)
),
)
);
$posts = $query3->posts; //Asigna a la variable $posts los posts que devuelve la consulta WP_Query anterior.
$count = count($posts); //Cuenta los posts que tiene la variable $count.
if ($count==3){ //Si el número de post contados es = 3 aumenta la variable contador en +1
$contador=$contador+1;
}
}
return $contador; //Devuelve la variable contador, que será el número de CPT Grupo que tienen 3 CPT Contrataciones CHILD.
}
add_shortcode ('grupo_tres_contrat', 'jm_grupo_tres_contrat');
But If I change the query to SQL the shortcode return 5:
function jm_grupo_tres_contrat() {
global $wpdb;
// Consulta SQL para contar los grupos con exactamente 3 contrataciones
$sql = "
SELECT COUNT(*) FROM (
SELECT pm.meta_value AS grupo_id
FROM {$wpdb->posts} AS p
INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
WHERE p.post_type = 'contrataciones-1'
AND p.post_status IN ('publish', 'pending')
AND pm.meta_key = '_wpcf_belongs_grupo_id'
GROUP BY pm.meta_value
HAVING COUNT(*) = 3
) AS resultados
";
$contador = $wpdb->get_var($sql);
return $contador;
}
add_shortcode('grupo_tres_contrat', 'jm_grupo_tres_contrat');
Why is the result different? Thanks
There are different results between wordpress and phpmyadmin as you can see:
Hi,
Thank you for contacting us and I'd be happy to assist.
Based on what you've shared, it seems the difference is caused by the fact that the first method of using the 'get_posts' function only queries the 'published' status posts, by default ( i.e. unless the 'post_status' is specified ).
( ref: https://developer.wordpress.org/reference/functions/get_posts/#comment-6670 )
I'll recommend printing the post IDs of the returned results from both cases and then checking their statuses in the databases.
regards,
Waqar
Both queries have the same post status: pending and publish
I couldn't reproduce the same on my test website.
Can you please share the temporary admin login details of the website where this difference can be seen, along with the exact steps?
I'll also need your permission to download the clone/snapshot of the website, for database-level troubleshooting.
Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.