Skip Navigation

[Geschlossen] different results with custom query

This support ticket is created vor 3 Monate, 1 Woche. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Waqar vor 3 Monate.

Assistiert von: Waqar.

Author
Artikel
#2692651

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

#2692654
Captura de pantalla 2024-04-16 142331.png
Captura de pantalla 2024-04-16 142422.png

There are different results between wordpress and phpmyadmin as you can see:

#2692754

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

#2692772

Both queries have the same post status: pending and publish

#2693546

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.

Das Thema „[Geschlossen] different results with custom query“ ist für neue Antworten geschlossen.