2

What I'm trying to do is alter the loop across my site so it will check the site URL so it will know which tag to display.

For example, if I give some posts the tag "golfclubs", I want it to work something like this:

if ($_SERVER['HTTP_HOST']=="www.golfclubreviewdomain.com"){
        // code here to display only the "golfclubs" tagged posts
}

In case the code above is incorrect for what I want, I just want to check the current domain to see if it is http://www.golfclubreviewdomain.com, and if so, display the posts that are tagged with "golfclubs". I would like this change to work across all themes, so I want to change this at highest level if possible so it will affect homepage, categories, etc. Is there a core file that I can use that will never be updated when I upgrade wordpress?

And my final question, what change do I make to display more than 1 type of tagged post? maybe something like "golfclubs" and "general"?

  • I assume this is multisite? – s_ha_dum Dec 15 '15 at 15:15
  • @s_ha_dum not exactly multisite, all sites will be controlled from 1 normal wordpress install – Joe Bobby Dec 15 '15 at 15:21
  • I wouldn't parse the URL like that, but since it isn't multisite I don't know how to guess at the controls you have in place. – s_ha_dum Dec 15 '15 at 15:25
  • @s_ha_dum how would u parse the URL? and I'm just looking for the basic way to display the loop based on 1 or 2 tags so just looking for the code to add into the brackets – Joe Bobby Dec 15 '15 at 15:29
  • I wouldn't parse the URL at all. You've got user data that requires validation and regex is always tricky. That is why I was looking for another way to approach it. – s_ha_dum Dec 15 '15 at 15:31
3
  • Create a must-use plugin.
  • Put it in <docroot>/wp-content/mu-plugins/selective-posts.php
  • Following should be the code in selective-posts.php to filter specific posts.

    function golfclubs_tag( $query ) {
        if ( $_SERVER['HTTP_HOST'] === 'www.golfclubreviewdomain.com' ) {
            $query->set( 'tag', 'golfclubs' );
        }
    }
    add_action( 'pre_get_posts', 'golfclubs_tag' );
    

This way it would filter all the queries across the site for specific domain and it would filter the posts with specific tag.

If you want to add another tag in the query, you can edit the above code as follows:

    function golfclubs_tag( $query ) {
        if ( $_SERVER['HTTP_HOST'] === 'www.golfclubreviewdomain.com' ) {
            $query->set( 'tag', 'golfclubs,general' );
        }
    }
    add_action( 'pre_get_posts', 'golfclubs_tag' );
  • thanks! i can get the first block of code to work, but with the 2nd 1 i get this: Warning: strpos() expects parameter 1 to be string, array given in /srv/users/s/wp-includes/query.php on line 1966 and Warning: preg_split() expects parameter 2 to be string, array given in /srv/users/s/wp-includes/query.php on line 1967 – Joe Bobby Dec 15 '15 at 18:24
  • Oops, Sorry ! My bad ! I've edited the answer. Remove the array and pass a comma separated string. – Udit Desai Dec 16 '15 at 19:46

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.