• Hi,
    I have a very unusual issue with wp_insert_post function.
    The functions working fine , but the problem is when
    I set category from the function wp_set_object_terms( $post_id, ,’any cat’, ‘product_cat’);
    its also set category on admin site.
    1-The problem is the category link https://khana.smartlifestore.com/product-category/uuuuuuuuu/
    not working working properly. means products are not showing on category page.
    2- And the funny part is when I update post from admin side products become visible on category page.
    All possible solutions:
    I also make the taxonomy template.
    deactivate all plugins
    change the theme.
    Still not working
    Please guide me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • https://khana.smartlifestore.com/product-category/hhhhh/ is working fine. As well as some of the other categories that I see defined. Are you sure you have a uuuuuuuuu category defined and, if so, any products assigned to it?

    Thread Starter hunnx77

    (@hunnx77)

    yes, i tell you earlier if I update product from admin side, category page will be shown.
    there is some problem with the term relationship or something like that. I also work on it but it is still not working.
    Can you send me the exact code for wp_insert_post.
    My code is given below.
    $my_post = array(
    ‘post_title’ => $rt_name,
    ‘post_content’ => $rt_desc,
    ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘product’
    );
    // Insert the post into the database
    $post_id = wp_insert_post( $my_post );

    wp_set_object_terms( $post_id, $tags, ‘product_tag’);

    $category_id = wp_set_object_terms( $post_id, $rt_name, ‘product_cat’);
    //$dish_id = wp_set_object_terms( $post_id, $rt_name, ‘dishes’);
    $cat_id=$category_id[0];
    //$dish_arr_id=$dish_id[0];

    wp_set_post_terms( $post_id, $cat_id, ‘product_cat’);

    $category_link = get_term_link( $cat_id, ‘product_cat’ );
    //$dish = get_term_link( $dish_arr_id, ‘dishes’ );
    //update_post_meta( $post_id, $key, $rt_name );
    update_post_meta( $post_id,’_price’, $rt_price );
    update_post_meta( $post_id,’_regular_price’, $rt_price );
    update_post_meta( $post_id, ‘ingredients’, $rt_ingredients );
    global $wpdb;
    $table_name = ‘wp_term_relationships’;
    $results = $wpdb->get_results(“SELECT term_id FROM wp_term_taxonomy WHERE taxonomy = ‘product_type’LIMIT 0 , 1″);
    //$taxonomy_id=$results->term_id;
    $taxonomyobj=$results[0];
    print_r($taxonomyobj);
    $taxonomy_id=$taxonomyobj->term_id;

    $wpdb->insert(
    $table_name,
    array(
    ‘object_id’ => $post_id,
    ‘term_taxonomy_id’ => $taxonomy_id
    ),
    array(
    ‘%d’,
    ‘%d’
    )
    );
    $wpdb->insert(
    $table_name,
    array(
    ‘object_id’ => $post_id,
    ‘term_taxonomy_id’ => $cat_id
    ),
    array(
    ‘%d’,
    ‘%d’
    )
    );

    I am havin the exact same issue.
    wp_set_object_terms for category works, the product is listed admin side, the single product view shows the category, everthing is happy… except…
    the category list page does not show the product…

    I have just started investigating… did you find anything?
    Thank you

    found out and it’s kinky… the query performed to fetch the category page product id’s is:
    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
    FROM wp_posts
    INNER JOIN wp_term_relationships
    ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_postmeta
    ON ( wp_posts.ID = wp_postmeta.post_id )
    INNER JOIN wp_postmeta AS mt1
    ON ( wp_posts.ID = mt1.post_id )
    WHERE 1=1
    AND ( wp_term_relationships.term_taxonomy_id IN (1290) )
    AND ( wp_postmeta.meta_key = ‘total_sales’
    AND ( ( mt1.meta_key = ‘_visibility’
    AND CAST(mt1.meta_value AS CHAR) IN (‘visible’,’catalog’) ) ) )
    AND wp_posts.post_type = ‘product’
    AND (wp_posts.post_status = ‘publish’
    OR wp_posts.post_status = ‘private’)
    GROUP BY wp_posts.ID
    ORDER BY wp_postmeta.meta_value+0 DESC, wp_posts.post_date DESC

    so in my case the products are not showing because there was not meta_key ‘total_sales’.
    creating this meta key on product creation (you can try setting directly on the DB) solved it.

    don’t ask me why the heck the category page query needs the total_sales meta…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Products are not showing on product-category Page’ is closed to new replies.