Forums

[resolved] Query Tags From Text Form (28 posts)

  1. thebillevard
    Member
    Posted 1 year ago #

    Hi, First of all, I'd like to thank everyone in advance for taking the time to read this, and hopefully help me out.

    I have a form with 2 fields:

    <form action="http://www.mysite.com/blah" method="post">
    <input type="text" name="VST1" id="VST1" /><input type="text" name="VST2" id="VST2" />
    <input type="submit" value="ok"/>
    </form>

    I'd LIKE to create a function that:

    1. fetches the two values from the form (VST1 & VST2)
    2. query posts to see if any have these 2 exact values as tags
    3. if post exists with these two tags, it exits function and displays post
    4. if post with tags does not exist, it exits and displays another url

    Any help with putting together this function/query would be greatly appreciated.

    Thanks!

  2. thebillevard
    Member
    Posted 1 year ago #

    ok, so I have the first part of the function working (1 + 2), which is:

    <?php
     $tgs = array();
     $tgs[] = get_query_var('VST1');
     $tgs[] = get_query_var('VST2');
     $tags = implode(",",$tgs);
    query_posts('tag='.$tags);
    ?>

    now I just need to work it out so it displays the entire post, not just the post url, and then I'll go from there. Any ideas?

  3. taina928
    Member
    Posted 1 year ago #

    Hi,
    how can I also implement pagination?

    Please help me..
    Thanks!!

  4. taina928
    Member
    Posted 1 year ago #

    I noticed that the query works perfectly well but if I divide pages for the search result pages do not work ..

  5. Mark / t31os
    Moderator
    Posted 1 year ago #

    Merge your query parameters with those that exist for the current query already, ie. paging values, etc..

    <?php
    // Args as array
    $args = array( 'tag__in' => array(
         get_query_var('VST1'),
         get_query_var('VST2')
    ) );
    // Merge args with existings args
    $args = array_merge( $args, $wp_query->query );
    
    // Pass args into query
    query_posts( $args );
    ?>

    Hope that helps..

  6. taina928
    Member
    Posted 1 year ago #

    Thanks Mark...
    the code that I've used is:

    <?php
    $tgs = array();
    $tgs[] = get_query_var('VST1');
    $tags = implode(",",$tgs);
    query_posts('tag='.$tags);
    ?>

    and it works fine but the pagination does not work, if I click on the second page I see the rest of the post but always those of the first page.

    excuse my English but I'm Italian :)

  7. taina928
    Member
    Posted 1 year ago #

    The result of the first page of this research is:
    http://www.artofweb.it/help/1.jpg

    The second this:
    http://www.artofweb.it/help/2.jpg

    as you can see nothing changes..

  8. Mark / t31os
    Moderator
    Posted 1 year ago #

    Did you try the code provided?

  9. taina928
    Member
    Posted 1 year ago #

    Yes I tried, but no longer even the tag ..

  10. taina928
    Member
    Posted 1 year ago #

    // Args as array
    $args = array( 'tag__in' => array(
         get_query_var('VST1')
    ) );
    // Merge args with existings args
    $args = array_merge( $args, $wp_query->query );
    
    // Pass args into query
    query_posts( $args );
    ?>
  11. Mark / t31os
    Moderator
    Posted 1 year ago #

    Are you registering the two query vars you're trying to fetch?

    VST1 and VST2..

    Does echo get_query_var('VST1'); give any result?

  12. taina928
    Member
    Posted 1 year ago #

    Hi Mark,
    I use 'VST1' will use only because I need only a search string!

  13. Mark / t31os
    Moderator
    Posted 1 year ago #

    If you put this code in your file.
    Translation: Se mettete questo codice, nel vostro file.

    echo get_query_var('VST1');

    Is a value shown on the page?
    Translation: È un valore indicato sulla pagina?

  14. taina928
    Member
    Posted 1 year ago #

    nothing, I always return the same result

  15. Mark / t31os
    Moderator
    Posted 1 year ago #

    Try using..

    $_POST['VST1']

    Is this code running on the page your form submits to? That's the URL the form's action points at..

    <form action="http://where?"
  16. taina928
    Member
    Posted 1 year ago #

    Thanks for the help Mark,
    but I can not figure out where I enter the code that I've posted. I attach the code I used to run my search page. "VST1"is replaced by "s". Anyway it works perfectly and I can only do research just like I wanted to tag, the only problem is paging.

    <form method="get" action="<?php bloginfo('url'); ?>/"><input type="text" size="20" class="search-field-teaser" name="s" id="s" value="" onfocus="if(this.value == '') {this.value = '';}" onblur="if (this.value == '') {this.value = '';}"/>
     <button type="submit"  value="Search" class="search-go fancy_button"><span>Cerca</span></button></p>
    </form>
    
    <?php
    $tgs = array();
    $tgs[] = get_query_var('s');
    $tags = implode(",",$tgs);
    query_posts('tag='.$tags);
    ?>

    Thanks again for your patience..

  17. taina928
    Member
    Posted 1 year ago #

    Or would you know you suggest a method to search only in the tags?

  18. Mark / t31os
    Moderator
    Posted 1 year ago #

    This code will search for tags that have a name like the search term, if any are found the query is updated to pull posts with those tags instead..

    $search_term = get_query_var('s');
    // If there's a search term
    if( $search_term ) {
    	// Look for tags with a name like the search term
    	$search_tags = get_terms( 'post_tag', array( 'fields' => 'ids', 'name__like' => '%' . like_escape( esc_sql( $search_term ) ) . '%' ) );
    	// If tags were found
    	if( !is_wp_error( $search_tags ) && !empty( $search_tags ) ) {
    		// Update the query to fetch posts that have those tags instead
    		$args = array_merge( array( 'tag__in' => $search_tags ), $wp_query->query );
    		// Reset the s query var so WordPress doesn't do the regular search query
    		$args['s'] = '';
    		// NOTE: You need the array merge to preserve other query parameters
    		query_posts( $args );
    	}
    }

    Tested the code to, so i know it works... ;)

  19. taina928
    Member
    Posted 1 year ago #

    I do not look for tags, but fishing in the entire content of the post..
    I'm going crazy ;(
    Maybe there is something else wrong ...
    How do I show the whole page?

    Thanks again

  20. taina928
    Member
    Posted 1 year ago #

    The code you sent me work!!!
    I just have to be able to remove the search in the content of the post, I need only search tags

  21. Mark / t31os
    Moderator
    Posted 1 year ago #

    The code i provided just goes into your search.php, it only searches tags, if it finds no matches for tags, then it doesn't do anything (ie. the normal search takes over).

    Can i see the code from the whole file you're doing this in please.
    http://wordpress.pastebin.com/

    Paste all the code from the file you are working on into a pastebin(link above) and report the link back here please.

  22. taina928
    Member
    Posted 1 year ago #

    Thanks Mark,
    one could not exclude the normal search for the tag and just leave?

    http://wordpress.pastebin.com/TSTXpDEv

  23. Mark / t31os
    Moderator
    Posted 1 year ago #

    When do you want the tag search code to run?
    Do you want to replace the regular search, or use tag search under a special condition?

    Translation (via the web):
    Quando vuoi il tag di eseguire codice ricerca?
    Vuoi per sostituire la ricerca regolare, o usare i tag ricerca in una condizione speciale?

  24. taina928
    Member
    Posted 1 year ago #

    I want to search only the tag!
    So that the search only works with those

  25. Mark / t31os
    Moderator
    Posted 1 year ago #

    Redirect to a 404 when no tags match?

    $search_term = get_query_var('s');
    // If there's a search term
    if( $search_term ) {
    	// Look for tags with a name like the search term
    	$search_tags = get_terms( 'post_tag', array( 'fields' => 'ids', 'name__like' => '%' . like_escape( esc_sql( $search_term ) ) . '%' ) );
    	// If tags were found
    	if( !is_wp_error( $search_tags ) && !empty( $search_tags ) ) {
    		// Update the query to fetch posts that have those tags instead
    		$args = array_merge( array( 'tag__in' => $search_tags ), $wp_query->query );
    		// Reset the s query var so WordPress doesn't do the regular search query
    		$args['s'] = '';
    		// NOTE: You need the array merge to preserve other query parameters
    		query_posts( $args );
    	}
    	else {
    		wp_redirect( get_bloginfo('home') . '/404' );
    	}
    }
  26. taina928
    Member
    Posted 1 year ago #

    Ok, it works correctly, it's perfect!
    Thanks Mark, really THANKS! :))

  27. Mark / t31os
    Moderator
    Posted 1 year ago #

    You're welcome... :)

  28. taina928
    Member
    Posted 10 months ago #

    Hi Mark,
    you know why this code does not work on wordpress 3.1.2?

    <?php
    $search_term = get_query_var('s');
    // If there's a search term
    if( $search_term ) {
    	// Look for tags with a name like the search term
    	$search_tags = get_terms( 'post_tag', array( 'fields' => 'ids', 'name__like' => '%' . like_escape( esc_sql( $search_term ) ) . '%' ) );
    	// If tags were found
    	if( !is_wp_error( $search_tags ) && !empty( $search_tags ) ) {
    		// Update the query to fetch posts that have those tags instead
    		$args = array_merge( array( 'tag__in' => $search_tags ), $wp_query->query );
    		// Reset the s query var so WordPress doesn't do the regular search query
    		$args['s'] = '';
    		// NOTE: You need the array merge to preserve other query parameters
    		query_posts( $args );
    	}
    
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic