Check in the_content if a string exists

Here is the situation.

I have setup a site using Magento-Wordpress integration. The integration works so I can call in the Magento site whatever I want from wordpress.

Read More

I want to show in the product page, a post from wordpress, that contains a specific word.
In my opinion, I have to search in the_content() of the posts the title of the product and then bring the post_meta I need.

The problem is that I cannot get it working.
I tried this:

<?php $name_of_product = $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
<?php echo $name_of_product ; ?>
<?php   $args = array( 'post_type' => 'avada_portfolio', 'posts_per_page' => 103 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();           

    <?php  
        $pos = strpos( get_the_content(), "[Dominos]" );
        var_dump($pos);
        if ( ! (FALSE == $pos) ) {  
        the_content();  
        the_title(); 
        }
        else{echo ("NOTHING HERE");}
    echo '</div>';
    endwhile;                                       
?>

but didn’t work.
Any suggestions?

Related posts

Leave a Reply

2 comments

  1. Based on your var_dump() output, you should be able to simply use:

    if ( strpos( get_the_content(), '13801580' ) !== false) {
        the_content();  
        the_title(); 
    }
    
  2. First check your $content, maybe it empty:

    <?php
    $args = array( 'post_type' => 'avada_portfolio', 'posts_per_page' => 103 );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();           
    
            $content = get_the_content();
    
            if($content){
              echo 'content present';
            }else{
              echo 'no content!!!';
            }
    
        endwhile;                                       
    

    if not empty and that is shortcode you can use next code for example:

     $content = '[mwi_product sku="13801580,13801584,13801584,13801578,13801580" title="true" title_tag="h2" desc="false" img="true" img_width="250" price="false" type="false" btn_color="blue" btn_link="button" cols="9"/]';
    
        $need_find = array('13801580', '13801584');
        foreach($need_find as $find){
          if(strpos( $content,  $find) !== false) { 
              echo 'Find your text: '.$find.'</br>';
          }
    
        }