Support » Fixing WordPress » wpdb->prepare() error

  • So, I’ve ready that this now needs a second argument. I’m no pro, and I have a code I’ve borrowed from someone else’s post to count things. However, its a couple years old and is missing the second argument.

    Can anyone plug the second argument into the code from what’s given in the code thus far?

    $term = get_term_by( 'slug', $term_slug, $taxonomy_type );
    
    $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts
    INNER JOIN $wpdb->term_relationships
    ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    INNER JOIN $wpdb->term_taxonomy
    ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_type = $post_type
    AND $wpdb->term_taxonomy.taxonomy = $taxonomy
    AND $wpdb->term_taxonomy.term_id = {$term->term_id};"));
Viewing 1 replies (of 1 total)
  • Try:

    <?php
    
    $term = get_term_by( 'slug', $term_slug, $taxonomy_type );
    
    $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts
    INNER JOIN $wpdb->term_relationships
    ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    INNER JOIN $wpdb->term_taxonomy
    ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_type = %s
    AND $wpdb->term_taxonomy.taxonomy = %s
    AND $wpdb->term_taxonomy.term_id = {%s->term_id};", $post_type, $taxonomy, $term));
    
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘wpdb->prepare() error’ is closed to new replies.