Access our premium support and let us know your problems, we will help you solve them.

0
No products in the cart.

Forum Replies Created

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • in reply to: Get ALL post types in WordPress in query_posts #9963
    blankdee_ell
    Participant
    'post_type' => 'any',
    

    This gets all posts except revisions. https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters

    So your query would be:

    query_posts(array(
        'post_type' => 'any',
        'meta_key' => 'post_views_count',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
            'post_type' => array('posttype1', 'postype2', 'posttype3')
        )
    );
    
    blankdee_ell
    Participant

    Sarakinos answer only worked with two small but important modifications.

    1. The $attribute->set_id() needs to be set to 0.
    2. The $attribute->set_name(); and $variation->set_attributes() do not need the pa_ prefix. The methods already handle the prefixes.

    So a working code would be:

    //Create main product
    $product = new WC_Product_Variable();
    
    //Create the attribute object
    $attribute = new WC_Product_Attribute();
    
    //pa_size tax id
    $attribute->set_id( 0 ); // -> SET to 0
    
    //pa_size slug
    $attribute->set_name( 'size' ); // -> removed 'pa_' prefix
    
    //Set terms slugs
    $attribute->set_options( array(
        'blue',
        'grey'
    ) );
    
    $attribute->set_position( 0 );
    
    //If enabled
    $attribute->set_visible( 1 );
    
    //If we are going to use attribute in order to generate variations
    $attribute->set_variation( 1 );
    
    $product->set_attributes(array($attribute));
    
    //Save main product to get its id
    $id = $product->save();
    
    $variation = new WC_Product_Variation();
    $variation->set_regular_price(10);
    $variation->set_parent_id($id);
    
    //Set attributes requires a key/value containing
    // tax and term slug
    $variation->set_attributes(array(
        'size' => 'blue' // -> removed 'pa_' prefix
    ));
    
    //Save variation, returns variation id
    echo get_permalink ( $variation->save() );
    
    
    // echo get_permalink( $id ); // -> returns a link to check the newly created product
    

    Keep in mind, the product will be bare bones, and will be named ‘product’. You need to set those values in the $product before you save it with $id = $product->save();.

Viewing 2 posts - 1 through 2 (of 2 total)