0

i have a ~46k rows in my wp_posts. All i want to do is to query the database that meet my queryes and display the resulted posts but my problem is that any query that i`m doing to the database it display only the last 5 entryes that were added. Thats the code:

if(isset($_GET["submit"])){

$nume_searchq=$_GET["nume_doc"];
$spec_searchq=$_GET["specializare_doc"];
$instit_searchq=$_GET["spital_doc"];
?>

<hr>
<strong>Rezultatele cautarii:</strong>
</br>

<?php

$query= $wpdb->get_results("SELECT * FROM `wp_posts` WHERE post_content like '%Alba%' OR LOWER(post_title) LIKE '%$nume_searchq%' OR LOWER(post_content) LIKE '%$spec_searchq%' OR LOWER(post_content) LIKE '%$instit_searchq%' ".$limit, OBJECT);
 $pageposts =get_posts($query);
if($pageposts){
    foreach($pageposts as $post){
                    global $post;
                    setup_postdata($post);
                    echo '<li><a href="' . get_the_permalink() . '"> ' . get_the_title() . ' </a></li>';
wp_reset_postdata();
}//end_Foreach
}//end_if
?>

I have tried different methods and queryes and still the most recent 5 entryies it display...how can i fix this? PS: dont bother about the $limit variable because i have a pagination code under this query. Thanks!

LE: i changed the code a bit and i have results but in my query i only need posts and it display absolutely everything that it found in the db, including small pictures, contact form, different pages, different codes, posts etc. Code looks like this for the moment:

    <?php 
   switch($_GET['filter1']){


   case "ALL":
        $querystr = "SELECT * FROM `wp_posts` WHERE post_type='post' OR LOWER(post_title) LIKE '%$nume_searchq%' OR LOWER(post_content) LIKE '%$spec_searchq%' OR LOWER(post_content) LIKE '%$instit_searchq%' ";
          break;
       case "AB":
          $querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Alba%' AND post_type='post' OR LOWER(post_title) LIKE '%$nume_searchq%' OR LOWER(post_content) LIKE '%$spec_searchq%' OR LOWER(post_content) LIKE '%$instit_searchq%' ";
         break;
    ..... //more case`es }

   $pageposts = $wpdb->get_results($querystr, OBJECT); ?>
 <?php if ($pageposts): ?>
 <?php foreach ($pageposts as $post): ?>
 <?php setup_postdata($post); ?>

 <div class="post" id="post-<?php the_ID(); ?>">
  <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    <?php the_title(); ?></a></h2>
    <?php //display_rating_result(); ?>
    <div class="entry">
      <?php the_content('Read the rest of this entry »'); ?>
    </div>
    <?php edit_post_link('Edit', '', ' | '); ?>  
    <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
  </div>
  <?php endforeach; ?>
 <?php else : ?>
   <h2 class="center">Not Found</h2>
 <?php endif; 


 }//end_submit 
 ?>
1

get_posts() doesn't accept posts as input, it only takes argument to query posts. So it's useless and breaking step in your process. It should work if you throw get_posts() out.

See Displaying Posts Using a Custom Select Query in Codex for extensive writeup on custom queries.

PS your query is seriously insecure, you need to be escaping any untrusted inputs.

  • Nop, it doesnt work. It display nothing, a blank screen. And from that place i took it and i changed it a bit. – Frapping Aug 6 '14 at 11:00
  • @Frapping please update your question with new code if you are trying something different – Rarst Aug 6 '14 at 11:02
  • Edited, check it out. – Frapping Aug 6 '14 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.