WordPress.org

Forums

add/remove filter for custom query (9 posts)

  1. blk784
    Member
    Posted 3 years ago #

    I am having a problem with a custom query I have for my homepage interferring with other pages. I have called the add_filter function in the functions.php within my theme folder and attempted to call remove_filter in my archive.php so that when a category is clicked on it will not use the filters. It simply gives me page not found. The site is 32bitsofwin.com. Any help is greatly appreciated! Thanks!

  2. vtxyzzy
    Member
    Posted 3 years ago #

    One solution I have used is to declare a global variable and set it just before the query. Then, declare the global variable in the filter and only apply changes if the filter is set. Finally, unset the variable right after the query.

  3. blk784
    Member
    Posted 3 years ago #

    I have set a global variable in the functions.php like so:

    global $sort;
    
    if (! isset($_GET['sort']) && empty($sort)) {
    $sort = 1;
    }
    
    if (isset($_GET['sort'])) {
    $sort = $_GET['sort'];
    }

    Then in the first line of my sidebar.php I set $sort = 0 so that the sort filters are not added if the sort is equal to 0. I have another code block that handles this in functions.php that looks like:

    if ($sort == 1 || $sort == 2){
      add_filter('posts_join', 'votes_join');
      add_filter('posts_where', 'votes_where');
      add_filter('posts_orderby', 'votes_orderby');
    }
    if ($sort == 0) {
    remove_filter('posts_join');
    remove_filter('posts_where');
    remove_filter('posts_orderby');
    }

    Am I missing something? still doesn't seem to work cause certain posts aren't showing up in my admin menu, only those returned by by the filters.

  4. vtxyzzy
    Member
    Posted 3 years ago #

    The global must be declared inside the filter functions. If declared in the body of functions.php, it gets tested before the filters are fired.

  5. blk784
    Member
    Posted 3 years ago #

    I have declared the global $sort inside my filters and check them inside the filters. How can I set it only if it is the homepage? any other page it can be empty and then the filters wont fire.

  6. vtxyzzy
    Member
    Posted 3 years ago #

    Test for is_front_page() and if true and the sort global is set, then do the filter.

  7. blk784
    Member
    Posted 3 years ago #

    Still not working! I have added this in each filter

    if(is_front_page() && empty($sort)) {
    filter code executed;
    }

    and these filters are at the bottom of my functions.php file. I also added this code to the top of my index.php file:

    global $sort;
    $sort = 1;

    so that on the first page request for the homepage the initial filter is fired. Also above the add_filter's in my functions.php
    I have:

    if(isset($_GET['sort'])){
    $sort = $_GET['sort'];
    }

    This should fire the filter if its the homepage or if the sort variable is populated in the $_GET array, otherwise it would be a normal page and the default query should run. When I click on posts that aren't in my custom fitlers, I get a page not found message and in the wp-admin dashboard for all posts, only posts from the filters are returned which is not correct. I am so desperate for help! Please dont get frustrated with me, I just don't know if I am adding the code in the right files.

  8. vtxyzzy
    Member
    Posted 3 years ago #

    Please put the code for your filters and the code in the template (where the global is given a value) in a pastebin, and post links to them here.

  9. blk784
    Member
    Posted 3 years ago #

    http://pastebin.com/F09vEkMp

    Here is the site and you will see the 3 filters on the right each one has a sort number and other than the homepage (which should default to $sort = 1) and these 3 menu items, any other page should load with the default query.

Topic Closed

This topic has been closed to new replies.

About this Topic