3

I have a query that looks like:

query_posts($query_string."&post_type=attachment&posts_per_page=9&paged=".$paged);

I'd like it to looks something like:

$args = array(
    'paged' => $paged,
    'posts_per_page' => 9,
    'post_type' => 'attachment'
);
query_posts($args);

I am trying to integrate $query_string into the query_posts with $args...can anyone point me in the right direction.

Thanks,
Josh

5
  • Without knowing what that variable contains it's not possible to answer the question. I'd also advise against using query_posts in favour of WP_Query for new queries, and the pre_get_posts filter for replacing/modifying the main query Commented Jul 31, 2017 at 13:46
  • $query_string contains the search query. Commented Jul 31, 2017 at 14:08
  • 1
    You mean a standard WP search? Be precise in the words you use so we don't misunderstand, if we can see an example that would be great Commented Jul 31, 2017 at 15:49
  • Yes, just the standard...I don't have that variable defined...but I am using it on another site - it wasn't defined there either. Basically, all it does (to my understanding) is carry over the search query so you can run a custom query on the search results page. If that can be done using another variable, that's fine too. Commented Jul 31, 2017 at 21:45
  • possibly the closest you can get: developer.wordpress.org/reference/functions/query_posts/…, or generally, consider to work with 'pre_get_posts' codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts Commented Aug 1, 2017 at 0:20

1 Answer 1

3

After some searching I found the parameters I needed: https://gist.github.com/luetkemj/2023628 (on Line 231)

//////Search Parameter
//http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
's' => $s,                              //(string) - Passes along the query string variable from a search. For example usage see: http://www.wprecipes.com/how-to-display-the-number-of-results-in-wordpress-search 
'exact' => true,                        //(bool) - flag to make it only match whole titles/posts - Default value is false. For more information see: https://gist.github.com/2023628#gistcomment-285118
'sentence' => true,                     //(bool) - flag to make it do a phrase search - Default value is false. For more information see: https://gist.github.com/2023628#gistcomment-285118

I added 's' => $s to my $args which passes along the query string, which is what I was looking for :-)

My code now looks like:

$args = array(
    'paged' => $paged,
    'posts_per_page' => 9,
    'post_type' => 'attachment',
    's' => $s
);
query_posts($args);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.