• Resolved jazzu

    (@jazzu)


    Hi!

    Like the title says, I can’t use the search function in wp-admin. If I go to products and use filters, the filters work. However if I type anything in the search bar, INCLUDING page number, I get a notification that there are no products. Even if leave the search bar empty and just click Search, it says that there are no products.

    I’m using the code below, however, if I have the code active, a lot of stuff doesn’t work properly anymore. For example, I can’t see the gallery to add images that were already uploaded, because it says that there are no pictures. It also messes up categories, and a bunch of other stuff (only in wp-admin). Even though these problems are only present in wp-admin, it’s very annoying that I have to add the /* and */ to the code so it’s disabled or enabled, depending of what I need at that moment.

    The Code:

    add_action( 'pre_get_posts', 'products_pre_get_posts' );
    function products_pre_get_posts( $query ) {
    
    if(is_admin()){
         $query->set( 'tax_query', array(
            array(
                'taxonomy'  => 'product_cat',
                'field'     => 'term_id',
                'terms'     => get_terms( array( 'taxonomy' => 'product_cat', 'fields' => 'ids' ) )
            )
        ));
      }
    }

    Kind regards

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

Viewing 12 replies - 1 through 12 (of 12 total)
  • Hi @jazzu

    Search function in wp-admin means the following. Is this where your search functionality is not working?

    IMAGE
    Link to image: https://snipboard.io/PY1K89.jpg

    I’m not sure why would you need to a code to make it work. It is a default feature.

    Kindly enlighten me on anything I’m missing so we can help you further.

    Cheers

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. Hopefully, the above info was helpful.

    If you have further questions, please feel free to open a new topic.

    Thread Starter jazzu

    (@jazzu)

    Hi @margaretwporg !

    I’m so sorry, I haven’t seen the email notification for this thread. I need to use the code, because whenever I use the search function (even if I leave the searchbox empty) it just says that no products have been found.

    Here you can see the page, when I just open the products tab.

    Here I just clicked on searchbox and pressed enter. It directly just displayed the message “Procuts not found”

    Kind regards

    Hi @jazzu

    Thanks for getting back.

    May I know what are you entering in the search box?

    For example, on my test site when I enter hoodie and click on the Search products button, I get a list of all product that have the phrase hoodie in the title.

    Please see the image below for reference:

    IMAGE
    Link to image: https://snipboard.io/8bRKt4.jpg

    I hope this helps!

    Thread Starter jazzu

    (@jazzu)

    Hi @margaretwporg !

    In the screenshots above, I just left it blank and tried to search. In the first screenshot, you can see, that one of the products is “Cev plavajo?a ?38mm”. If I write “Cev” in the searchbox, it just says No products have been found.

    Hello,

    Sorry to hear that.

    There is an alternative solution in this page, please check.

    It is highly recommended to use a plugin like Code Snippets to add custom PHP code into your site without directly accessing the functions.php file. Here is an article that explains how to use the Code Snippets plugin.

    Let us know how it goes.

    Thread Starter jazzu

    (@jazzu)

    Hi @ihereira

    No, the problem is not about the functions.php file, I have it in my child theme, I’ve never written anything in the original file. The code is also not a problem, it’s just annoying because I can’t access the files I have already uploaded on the website, because it says it’s empty, etc., so what I need to do every time is to add the /* at the start of the code.

    So basically what I mean by that is when I need to search for products or categories, I need to use the code below

    /*		Search - ADMIN		*/
    
    add_action( 'pre_get_posts', 'products_pre_get_posts' );
    function products_pre_get_posts( $query ) {
    
    if(is_admin()){
         $query->set( 'tax_query', array(
            array(
                'taxonomy'  => 'product_cat',
                'field'     => 'term_id',
                'terms'     => get_terms( array( 'taxonomy' => 'product_cat', 'fields' => 'ids' ) )
            )
        ));
      }
    }

    And when I find the products or categories, I need to update the code by just adding the /* to the top so it doesn’t work anymore:

    /* Search - ADMIN */ /*

    As you can probably imagine, it’s time consuming when I’m editing 500+ products.

    Kind regards

    Hi @jazzu

    Thanks for clarifying.

    Kindly be informed that support for custom coding is beyond the scope of this forum. This particular forum is for questions that are related to the WooCommerce core features.

    We’ll be happy to investigate deeper if you experience the same behavior when your site is running the latest versions of WordPress, WooCommerce and our free Storefront theme as the only enabled software, and has no custom code added. This will entail disabling all plugins, disabling all custom code, and switching to a basic theme like Storefront. Please let us know.

    For help with custom code I recommend the WooCommerce Developer Resources Portal.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Lastly, for direct assistance with code customizations, I recommend consulting with the WooCommerce Customizations Partners. https://woocommerce.com/customizations/`

    I hope you find this information helpful.

    Cheers

    Thread Starter jazzu

    (@jazzu)

    Hi @margaretwporg

    Yeah, I tried it. It’s only happening when I’m using my child theme..

    So I did some digging in my functions.php. The code below is used so that products are not displayed in the actual store outside of subcategories. This is the code that is causing the problems:

    function exclude_product_cat_children($wp_query) {
    if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query()) {
        $wp_query->set('tax_query', array(
                                        array (
                                            'taxonomy' => 'product_cat',
                                            'field' => 'slug',
                                            'terms' => $wp_query->query_vars['product_cat'],
                                            'include_children' => false
                                        )
                                     )
        );
      }
    }
    
    add_filter('pre_get_posts', 'exclude_product_cat_children');

    Update: Great news! While testing this stuff, my store got all messed up. Footer content got deleted somehow (widgets are gone), the main shop page got messed up..
    Just great! ??

    Kind regards

    • This reply was modified 2 years, 9 months ago by jazzu.
    • This reply was modified 2 years, 9 months ago by jazzu.

    Hi @jazzu

    It’s only happening when I’m using my child theme

    It’s great that this could be detected and the particular code that is causing the problem could be pin-pointed. So you may want to modify or exclude the code as appropriate.

    While testing this stuff, my store got all messed up

    I can understand the inconvenience caused, but it was worthwhile as that helped find the culprit. In future, for any such tests, if you’d prefer not to make any changes on your live site then you can create a duplicate/staging site. Many hosting providers offer site staging facilities, but if you don’t have such a feature, you can create one with the WP Staging plugin.

    Cheers

    Thread Starter jazzu

    (@jazzu)

    Hi @margaretwporg !

    Thank you for your reply. Yeah, I guess that it’s okay that I pin-pointed it, however, I don’t know why that code would prevent anything in admin dashboard as I’m not good with php, so the only thing I gave myself was some extra work ?? It’s already fixed though.

    Kind regards

    Thank you for letting us know!

    I’ll mark this thread as resolved now. If you have any further questions, I recommend creating a new thread.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Product search not working in wp-admin’ is closed to new replies.