• Resolved benutzerfreund

    (@benutzerfreund)


    I want to show all posts of a specific category containing a custom keyword in the post title.

    What works is:

    <?php
    		$args = array('category_name' => 'interviews', 'posts_per_page' => '999' );
    	    $the_query = new WP_Query( $args );
    		while ( $the_query->have_posts() ) {
    			$the_query->the_post();
    			?>
    			<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    			<?php
    		} ?>

    But when I add `’s’ => ‘peter’ to $args, nothing is found (to get only interviews with Peter). Using only ‘s=peter’ does’t do the trick also.
    But searching for ‘peter’ with the standard search box does bring up the correct posts.

    Any hints?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Using your code on my site works just fine. I only changed the assigned ‘category_name’ and ‘s’ values to work with my post data. I’m guessing you have a theme or plugin that is discarding the search argument except for actual search queries.

    BTW, always try to use the array form of arguments 's' => 'peter' in preference to the string form 's=peter'. But never mix forms. If you are dealing with existing code using the string form, you must also use the string form for additions unless you rewrite the whole thing to be in the array form. Which could be worth doing, the string form could be deprecated some day.

    Thread Starter benutzerfreund

    (@benutzerfreund)

    Ah, thanks a lot for the tip!

    In fact I use the plugIn Relevanssi on my site. When I deactivate it, the function works as it should.

    Any idea how I can use Relevanssi and my own code alongside? The standard search of WP is … not so good.

    Thread Starter benutzerfreund

    (@benutzerfreund)

    Oh my, I should think before replying. You pointed me in the right direction – on the Relevanssi-page everything is spelled out.

    You just have to insert the line
    relevanssi_do_query($the_query);
    in the code above (before the ‘while’).

    Thanks a lot!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show only posts with specific keyword in title’ is closed to new replies.