• I have a problema to solve in WordPress. The “recent post” of my frontpage gets all posts, doesnt matter the type of post. It gets all.

    I use category_id to create sections of new, articles, site, etc.

    But I want to filter the main latest post of my frontpage only with the category id 34.

    The problem is cause the index.php file uses the block to all others “sections” (parent categories). So I need to alter the index.php to work as if they are in frontpage and with no other parameters (to the main frontpage block), it should show only the category id 34 (news). But if when in other folders/url with parameters, must follow the category of the parameter as it is today.

    The block I have in index.php is that:

      if ( have_posts() ) :
    

    while ( have_posts() ) :

               the_post();
    

    ct_mission_news_get_content_template();

           endwhile;
    

    endif;

    If I change here, it will reflect in all section/folders that uses category as filter.
    So, how could I make the rule to filter post with category id 34 only if its in firstpage and theres no other parameters (there are blocks in first page that uses this block to, so it should be able to work if there is any parameter.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to alter the query made for the most recent post. This can be done through the “pre_get_posts” action. The trick will be altering the right query without affecting other queries. If this is the main query for the page you can use its is_front_page() method to check. If not you’ll need to be more creative. One possibility is logic similar to:

    • Add action callback limiting queries to a category
    • Make the query
    • Remove the previously added action callback

    In the action callback, use the passed object’s set() method to set the “cat” query var to the desired category ID.

Viewing 1 replies (of 1 total)
  • The topic ‘How to filter recent post by category only in first page block ?’ is closed to new replies.