Den Überblick bei Beiträgen oder Produkten in WordPress zu behalten wird mit wachsender Anzahl schwieriger. Manchmal machen Zuständige auch Fehler, die mithin erst in bestimmten Situationen oder lange Zeit später in den Fokus geraten.
In solchen Fällen sind Prüffunktionen hilfreich, die zusammenfassen, an welchen Stellen Abweichungen bestehen, zum Beispiel bei welchen Produkten in WooCommerce das Produktbild fehlt. Der Query lässt sich für beliebige Zwecke anpassen.
Als Ergebnis wird hier eine Liste mit Links zu betroffenen Produkten ausgegeben.
function flxo_products_missing_featured() {
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS'
),
),
'posts_per_page' => -1,
'fields' => 'ids'
);
/* the custom query object */
$searched_posts = new WP_Query( $args );
if ( !count( $searched_posts->posts ) ) {
wp_reset_postdata();
return false;
}
$links = array();
/* get only post ids from object */
foreach ( $searched_posts->posts as $id ) {
$links[] = sprintf('<a href="%1$s">%2$s</a>', get_the_permalink( $id ), get_the_title($id));
}
wp_reset_postdata();
$linklist = implode('<br />', $links );
return $linklist;
}
oder mit get_posts()
function flxo_products_missing_featured() {
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS'
),
),
'posts_per_page' => -1,
'fields' => 'ids'
);
/* the custom query object */
$searched_posts = get_posts( $args );
if ( !count( $searched_posts ) ) {
wp_reset_postdata();
return false;
}
$links = array();
/* get only post ids from object */
foreach ( $searched_posts as $id ) {
$links[] = sprintf('<a href="%1$s">%2$s</a>', get_the_permalink( $id ), get_the_title($id));
}
wp_reset_postdata();
$linklist = implode('<br />', $links );
return $linklist;
}
Schreibe einen Kommentar