How do I search for a word in a wordpress post?

9 replies
  • WEB DESIGN
  • |
How do I search for a word in a wordpress post?

For example, I would like to include a code that checks whether the word "apple" is found in a wordpress post.

I am not talking about a search box. I am talking about a code that runs in the index.php
#post #search #word #wordpress
  • Profile picture of the author davidfrankk
    Open the source of the page you want to check and look for the word. Does that work for you?
    {{ DiscussionBoard.errors[8285309].message }}
    • Profile picture of the author MindReality
      Originally Posted by davidfrankk View Post

      Open the source of the page you want to check and look for the word. Does that work for you?
      No. That is not what I mean.

      For example: I want the code to check if the word "apple" appears in a post and display a text that says "This post contains the word apple" below the content.
      Signature
      Discover The Greatest Secrets Of The Mind And Reality That Will Get You Anything You Desire, Almost Like Magic! Visit: http://www.MindReality.com
      {{ DiscussionBoard.errors[8285326].message }}
  • Profile picture of the author Istvan Horvath
    Originally Posted by MindReality View Post

    I am talking about a code that runs in the index.php
    Well, in your index.php (and any PHP file, for that matter) there is no content ever - only php and html code, which means there are no words... except for the occasional comment on code.

    Your content - like posts, Pages, comments - are stored in the MySQL database, not in the files.

    EDIT. If you're still talking about files, any decent text editor has a search function, where you can search (offline of course!) in your files for words, strings, characters etc.
    Signature

    {{ DiscussionBoard.errors[8285334].message }}
    • Profile picture of the author MindReality
      Originally Posted by Istvan Horvath View Post

      Well, in your index.php (and any PHP file, for that matter) there is no content ever - only php and html code, which means there are no words... except for the occasional comment on code.

      Your content - like posts, Pages, comments - are stored in the MySQL database, not in the files.

      EDIT. If you're still talking about files, any decent text editor has a search function, where you can search (offline of course!) in your files for words, strings, characters etc.

      My index.php contains this code:

      Code:
      <div id="content">
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      	<?php // Post dates off by default the_date('','<h2>','</h2>'); ?>
      	<h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>	
      	<div class="meta">Posted by <?php the_author() ?> <?php edit_post_link(__('Edit This')); ?> - <a href="http://www.mindreality.com/recform.php" target="page" onClick="window.open('','page','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=550,height=450,left=50,top=50,titlebar=yes')">Share this article with friends!</a> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<?php if(function_exists('wp_print')) { print_link(); } ?>
      </div>
      <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
      	<div class="main">
      		<?php the_content(__('(more...)')); ?>
      	</div>
      <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
      <?php
      if (!is_user_logged_in()) { ?>
      <div align="center"><b>Wait, this is just PART of the secrets revealed... There is MUCH vital information you are missing... <a href="http://www.mindreality.com/view.html">Click Here to View the HIDDEN PART!</a></b></div>
      <?php } ?>
      As you can see, the sentence "Wait, this is just PART of the secrets revealed... There is MUCH vital information you are missing... Click Here to View the HIDDEN PART!" only shows up if the user is not logged in.

      How can I also make sure that the sentence only displays if there is a word "[hidepost]" in the post content?
      Signature
      Discover The Greatest Secrets Of The Mind And Reality That Will Get You Anything You Desire, Almost Like Magic! Visit: http://www.MindReality.com
      {{ DiscussionBoard.errors[8285340].message }}
  • Profile picture of the author Istvan Horvath
    OK, your question is NOT about searching the content... that's for starters.

    And what was the answer of the theme author/creator/seller when you asked for help?

    I suspect you'd like to "combine" two incompatible features: a conditional tag in a WP theme file and a shortcode of a plugin that is meant to hide part of the post.

    For a coder there may be a solution - although unrelated to your idea of "searching" for a word. That's not how things work.

    Go over to the Programming section and ask the right question!
    Signature

    {{ DiscussionBoard.errors[8285411].message }}
    • Profile picture of the author MindReality
      Originally Posted by Istvan Horvath View Post

      OK, your question is NOT about searching the content... that's for starters.

      And what was the answer of the theme author/creator/seller when you asked for help?

      I suspect you'd like to "combine" two incompatible features: a conditional tag in a WP theme file and a shortcode of a plugin that is meant to hide part of the post.

      For a coder there may be a solution - although unrelated to your idea of "searching" for a word. That's not how things work.

      Go over to the Programming section and ask the right question!
      No, no.

      I am not trying to combine anything at all.

      I am indeed trying to search for a word in a post. Even the hidepost tag is in the form of a word in the post.

      The code has to look through the entire post content to find whether that word exist in it, and to display a text that says so.
      Signature
      Discover The Greatest Secrets Of The Mind And Reality That Will Get You Anything You Desire, Almost Like Magic! Visit: http://www.MindReality.com
      {{ DiscussionBoard.errors[8285430].message }}
  • Profile picture of the author Istvan Horvath
    Again... you are NOT talking about "search" as it is understood generally.

    You want to programatically do something like:

    if this condition is true > then perform the following action

    and that is NOT a design question, so go over to the Programmers... or if you still insist here, I can delete the whole thread
    Signature

    {{ DiscussionBoard.errors[8285602].message }}
  • Profile picture of the author Kael41
    As a programming overlay, this is twofold: one, you would need a function which calls into the mysql db to "search" the post field for that specific post in the tablespace to see if the word apple exists. Then, you would need a call in the php code itself to call that function which checks the tablespace and then outputs that the term "apple" exists. Makes sense?
    {{ DiscussionBoard.errors[8290515].message }}
    • Profile picture of the author MindReality
      Originally Posted by Kael41 View Post

      As a programming overlay, this is twofold: one, you would need a function which calls into the mysql db to "search" the post field for that specific post in the tablespace to see if the word apple exists. Then, you would need a call in the php code itself to call that function which checks the tablespace and then outputs that the term "apple" exists. Makes sense?
      Thank you, this was something I thought of at first. No one else suggested it yet. Could you elaborate more on this at this thread:
      http://www.warriorforum.com/programm...ress-post.html
      Signature
      Discover The Greatest Secrets Of The Mind And Reality That Will Get You Anything You Desire, Almost Like Magic! Visit: http://www.MindReality.com
      {{ DiscussionBoard.errors[8291618].message }}

Trending Topics