php – Why is my array_diff usage breaking things?


On my WP site, I usually only have one tag per post.

However, I now have some posts that have a second tag, “expired”, that I use for another purpose.

So I need to adjust my code below to remove that “expired” tag before I process the one tag left.

It looks like I should be able to use array_diff to remove the “expired” tag, based on my reading of and some other stack exchange posts.

However, when I tried to implement that, I’m getting this error: Fatal error: Uncaught Error: Object of class WP_Term could not be converted to string

Can you show me where I’m going awrong here? 🙂

//get topic (tag)
    global $post;       
    $post_id = $post->ID;
    $entry_id = FrmDb::get_var( 'frm_items', array( 'post_id' => $post_id_here ), 'id' ); //get entry id
    $topic = get_the_tags($post_id); //get topic (tag) 

//here's what I'm trying to add

    $topic = array_diff($topic, ["expired"]); //remove expired tag if an expired post

    $topic = $topic[0]->name; //should only be one
    $topic_url = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $topic)));
    $image_id = get_post_thumbnail_id();
    $image_src = wp_get_attachment_image_src( $image_id, 'full' );
    $img_url = $image_src[0]; // image url
        if (!empty($topic)) {
$see_more="<div id="topic"><a href="".$topic_url.'/">See more from our '.$topic.'</a></div>';
        else $see_more = "";

Chris 9 months 2022-09-20T17:17:26-05:00 0 Answers 0 views 0

Leave an answer