• Resolved thhhhh

    (@thhhhh)


    Hi all,

    I’m trying to run a function to alter the post_type in my wordpress query string (or to add a new post type altogether). The function I’m using comes from here: https://www.addedbytes.com/blog/code/php-querystring-functions/

    Based on the examples in this thread, I’ve written a sidebar that looks like this:

    <?php //add query to URL
    function add_querystring_var($url, $key, $value) {
        $url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
        $url = substr($url, 0, -1);
        if (strpos($url, '?') === false) {
            return ($url . '?' . $key . '=' . $value);
        } else {
            return ($url . '&' . $key . '=' . $value);
        }
    }
    ?>
    <?php 
    
    if (isset($_GET['run'])) $linkchoice=$_GET['run'];
    else $linkchoice=''; 
    
    switch($linkchoice){ 
    
    case 'showAll' :
        add_querystring_var($url, 'post_type', '');
    	break; 
    
    case 'showAnimation' :
        add_querystring_var($url, 'post_type', 'animation');
        break; 
    
    case 'showPeople' :
        add_querystring_var($url, 'post_type', 'people');
        break; 
    
    case 'showStudios' :
        add_querystring_var($url, 'post_type', 'studios');
        break; 
    
    case 'showResources' :
        add_querystring_var($url, 'post_type', 'resources');
        break; 
    
    case 'showWiki' :
    	add_querystring_var($url, 'post_type', 'wiki');
    	break;
    default : ;
    } 
    
    ?> 
    
    <h2>Show... </h2>
    <a href="?run=showAll">All</a>
    <a href="?run=showAnimation">Animation</a>
    <a href="?run=showPeople">People</a>
    <a href="?run=showStudios">Studios</a>
    <a href="?run=showResources">Resources</a>
    <a href="?run=showWiki">Wiki</a><br /><br />

    When I click any of these links, they take me to “mywebsite.com/?run=showX” – which in turn directs me to my front page. I think this means that the function is not running, and I’m wondering if anyone has any suggestions as to why. I’ve tried testing without the function, and with just echo functions generating text to indicate that a function has ran – and these haven’t done anything either. I still am directed to the url indicated in the link and nothing beyond that seems to happen. Have I set the function to run incorrectly?

    Thanks in advance for any help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter thhhhh

    (@thhhhh)

    Does anyone have any experience doing this? I’m willing coding strategy if I can accomplish this another way. I don’t really know AJAX but it seems to me like this piece of code here is doing nothing:

    if (isset($_GET['run'])) $linkchoice=$_GET['run'];
    else $linkchoice='';
    Thread Starter thhhhh

    (@thhhhh)

    Hi all,

    I’ve rewritten this a bit to see if I can get it to work, following jquery ajax tutorials.

    I have moved the PHP function I want to execute to my functions.php file. My sidebar now looks like this:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
    </script>
    <h2>Show... </h2>
    <script type="text/javascript">
    function showAll() {
        $.get("addquery.php", { value:"" });
        return false;
    }
    function showAnimation() {
        $.get("addquery.php", { value:"animation" });
        return false;
    }
    function showPeople() {
        $.get("addquery.php", { value:"people" });
        return false;
    }
    function showResources() {
        $.get("addquery.php", { value:"resources" });
        return false;
    }
    function showStudios() {
        $.get("addquery.php", { value:"studios" });
        return false;
    }
    function showWiki() {
        $.get("addquery.php", { value:"wiki" });
        return false;
    }
    </script>
    <a href="#" onclick="showAll();">All</a>
    <a href="#" onclick="showAnimation();">Animation</a>
    <a href="#" onclick="showPeople();">People</a>
    <a href="#" onclick="showStudios();">Studios</a>
    <a href="#" onclick="showResources();">Resources</a>
    <a href="#" onclick="showWiki();">Wiki</a><br /><br />
    <h2>Browse by topic... </h2>
    <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(7) ) : else : ?>
    <?php endif; ?>

    My “Addquery.php” file which I am using to execute the function looks like this:

    <?php
    $_GET['value'] = $value;
    add_querystring_var($url, 'post_type', $value);
    ?>

    But when I run the functions, nothing happens. I can see in chrome that the browser is trying to access “mysite.com/addquery.php?value=wiki” correctly and that it is receiving an error. The error is “Failed to load resource: the server responded with a status of 500 (Internal Server Error)”

    Does anyone have any suggestions for how I might proceed?

    Moderator keesiemeijer

    (@keesiemeijer)

    What is it exactly you want to do?

    for adding or removing a query variable to the URL you can use:
    https://codex.www.ads-software.com/Function_Reference/add_query_arg
    https://codex.www.ads-software.com/Function_Reference/remove_query_arg

    Thread Starter thhhhh

    (@thhhhh)

    keesiemeijer, thanks for responding! What I am trying to do is to make links in my sidebar which add a post_type argument to the existing query string of an archive page.

    I didn’t know about these functions, hence why I was using the other ones. But I will definitely try them out. They look like they might do exactly what I was hoping for.

    Thread Starter thhhhh

    (@thhhhh)

    I’ve put in the new function but I still can’t figure out how to trigger it… will keep plugging away

    Moderator keesiemeijer

    (@keesiemeijer)

    Try adding the links like this on your archive pages:

    <a href="<?php echo add_query_arg( 'myposttype', 'people' ); ?>">people<a>

    With this in your theme’s functions.php you can add the post type to the normal post type “post” that’s normally displayed on archive pages:

    function add_post_type($query) {
      if (!is_admin() && $query->is_main_query()){
        if (isset($_GET['myposttype']) && $_GET['myposttype']) {
          $post_types = get_post_types(array('public' => true, '_builtin' => false), 'names', 'and');
          if(!empty($post_types) && in_array($_GET['myposttype'], (array) $post_types)) {
    
            // add the post type from the url to the normal "post" post type
            $query->set('post_type', array('post', $_GET['myposttype']));
    
          }
        }
      }
    };
    add_action('pre_get_posts','add_post_type');

    Thread Starter thhhhh

    (@thhhhh)

    Amazing!! Works perfectly!!

    I was under the impression that a PHP function couldn’t be run straight out of an HTML link without a separate script or AJAX, but I guess I was mistaken! This is much cleaner (and more functional) than what I was attempting!

    Thanks so much!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you got it resolved ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Run PHP function from HTML hyperlink’ is closed to new replies.