• Resolved sdr2585

    (@sdr2585)


    The posts display in the sidebar properly, but when I use the plugin WP Hide Post to hide these “news” posts on the main blog page, the posts are no longer shown in the sidebar either. How do I fix this? I want them to show in the sidebar, but I use that plugin so that they don’t show on main blog page.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Aldo Latino

    (@aldolat)

    Hello,
    there is no solution because that plugin completely hides the posts from any query, as if the posts weren’t in the database. In this situation, WordPress and Posts in Sidebar cannot get hidden posts.

    The WordPress compliant way to obtain the result you want would be filtering the query of the main blog page, saying to WordPress to not get posts from the “News” category.

    Best regards.

    Thread Starter sdr2585

    (@sdr2585)

    How do I go about doing that? Is it difficult?

    Plugin Author Aldo Latino

    (@aldolat)

    No, it’s pretty simple.

    Tell me if “news” is a category or a tag and also the ID of that taxonomy.

    Thread Starter sdr2585

    (@sdr2585)

    News is a category and the ID is 10.

    Plugin Author Aldo Latino

    (@aldolat)

    First of all, deactivate the plugin WP Hide Post.

    Then, open the file functions.php of your current theme.

    Go to the end of the file.

    Look if the last line contains the symbol ?>. If it’s there, delete it and paste the following lines in its place:

    
    function my_customized_query( $query ) {
    	if ( $query->is_home() && $query->is_main_query() ) {
    		$query->set( 'cat', '-10' );
    	}
    }
    add_action( 'pre_get_posts', 'my_customized_query' );
    

    Save and upload the file to the server.

    This function will change the query of the main loop, removing the posts from the category “News”, while leaving the possibility to get posts from that category. Also, the function will work only in the blog main page, while leaving untouched other pages (taxonomy pages, search pages, ad so on).

    Let me know, please.

    • This reply was modified 8 years, 4 months ago by Aldo Latino.
    Plugin Author Aldo Latino

    (@aldolat)

    If you want to hide more categories, you can edit the function adding the IDs in a comma-separated list. For example:

    
    function my_customized_query( $query ) {
    	if ( $query->is_home() && $query->is_main_query() ) {
    		$query->set( 'cat', '-10,-11,-325' );
    	}
    }
    add_action( 'pre_get_posts', 'my_customized_query' );
    
    Thread Starter sdr2585

    (@sdr2585)

    It did end with the ?> so I took that out and put in what you told me, but it didn’t make any changes. This is what it looks like now…

    <?php
    register_sidebar( array(
    ‘name’ => __( ‘Left Sidebar Widget Area’, ‘squirrel’ ),
    ‘id’ => ‘left-sidebar-widget-area’,
    ‘description’ => __( ‘The first header widget area’, ‘squirrel’ ),
    ‘before_widget’ => ”,
    ‘after_widget’ => ”,
    ‘before_title’ => ‘<h2 class=”widget-title”>’,
    ‘after_title’ => ‘</h2>’,
    ) );

    register_sidebar( array(
    ‘name’ => __( ‘Header Widget Area’, ‘squirrel’ ),
    ‘id’ => ‘header-widget-area’,
    ‘description’ => __( ‘The header widget area’, ‘squirrel’ ),
    ‘before_widget’ => ”,
    ‘after_widget’ => ”,
    ‘before_title’ => ‘<h2 class=”widget-title”>’,
    ‘after_title’ => ‘</h2>’,
    ) );
    register_sidebar( array(
    ‘name’ => __( ‘CopyRight Footer Widget Area’, ‘squirrel’ ),
    ‘id’ => ‘cr-widget-area’,
    ‘description’ => __( ‘The CopyRight widget area’, ‘squirrel’ ),
    ‘before_widget’ => ”,
    ‘after_widget’ => ”,
    ‘before_title’ => ‘<h2 class=”widget-title”>’,
    ‘after_title’ => ‘</h2>’,
    ) );

    function my_customized_query( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( ‘cat’, ‘-10’ );
    }
    }
    add_action( ‘pre_get_posts’, ‘my_customized_query’ );

    Plugin Author Aldo Latino

    (@aldolat)

    The reason could be among these:
    1) we aren’t in home, where home is the page that shows the time-based blog content of the site;
    2) your posts are not retrieved in the main query but in some custom query (for example, you are using a page layout plugin that generates its own queries).

    The function has effect only if these two conditions are true.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Posts In Sidebar Display’ is closed to new replies.