WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Filter posts in list (6 posts)

  1. laqrhead
    Member
    Posted 5 years ago #

    I am trying to create a plugin that will only list posts in a category that meet a criteria I've defined in an external table.

    So far the admin section has gone ok, I have my list of criteria available to select and assign to posts.

    Now when a category page is displayed I only want to show posts that meet the criteria. So where or how do I intercept the list of posts and filter it before it displays?

    I thought I was onto something yesterday, but I can't remember it know.

  2. laqrhead
    Member
    Posted 5 years ago #

    I just read that, it seems a bit cryptic, so here is a bit more of what I'm trying to do.

    I want to set up one blog that displays on many sites. Certain posts will display on one or a few sites, and some will be on all sites.

    So what I've done is create a table with the list of sites (by domain) and a table that associates posts with sites.

    What I want to do is have only posts that have been assigned to that site display on that site.

    An example of how this might be used is these 4 domains:

    dogs.tld
    cats.tld
    hamsters.tld
    pets.tld

    There may be categories like

    Food
    Care & Grooming
    Cute Anecdotes
    Vet Tips

    These four different blogs have the same categories accross them all, but the blog posts wouldn't be appropriate for all of them all the time.

    A post about how to deal with pet stains in the carpet may be appropriate on all of the blogs, but a post on hairballs may only be appropriate on pets.tld and cats.tld.

  3. laqrhead
    Member
    Posted 5 years ago #

    It seems like the_posts hook is where I need to start, I think.

  4. laqrhead
    Member
    Posted 5 years ago #

    Well, I've figured this out, but it filters posts even in the admin panel. I'll have to figure that one out.

    add_filter('the_posts', 'my_function' , 1 );
    
    function my_function( $posts ) {
    
        // create an array to hold the posts we want to show
        $new_posts = array();
    
        //
        // loop through all the post objects
        //
        foreach( $posts as $post ) {
    
            $include = true;
    
            //
            // Perform Filter comparison
            //
            if ($filterPost>=1) {
                $include = false;
            }
    
            //
            // if we want to include it then
            // add the post object to the new posts array
            //
            if ( $include === true ) {
                $new_posts[] = $post;
            }
        }
    
        //
        // send the new post array back to be used by WordPress
        //
        return $new_posts;
    }
  5. stanford
    Member
    Posted 4 years ago #

    Have you figured this out? I have a similar issue.

  6. f00bar
    Member
    Posted 4 years ago #

    Not quite sure if anyone figured this out, as it was posted 6 months ago but I would suggest the following (even though perhaps a little hacky):

    if(!strpos($_SERVER["REQUEST_URI"],"wp-admin")){
       add_filter('the_posts','my_function');
    }

    What this is saying is, if the URL doesn't contain the standard "wp-admin" part, apply the filter. Otherwise, it won't filter them. I assume this is what you need? Obviously if you want it filtered on admin but not front end, remove the "!" just before the "strpos".

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.