• I have created a custom product attribute called course-name and I have a course where course-name is set to open-water-diver. I’d like to be able to query my products by that custom product attribute but I can’t find out how. In this example I’d like to find posts where course-name is open-water-diver. What I want to do is create a shortcode that queries by that custom product attribute and displays the product’s permalink.

    https://wordpress.org/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Could you switch it to a global attribute created at Products > Attributes and then use that instead of a custom attribute? Then you can use the layered nav widget for filtering / querying.

    Otherwise, this will require some custom code.

    Thread Starter SuperGraham

    (@supergraham)

    Yes, sure, but I want to keep this separate from variations, which I might use later. and I have most of the code I need… I just need the query array to work correctly. I’ve tried several options, but none seem to work.

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    function ow_url_func() {
    	$args = array (
    		'post_type'  => 'product',
    		'posts_per_page'  => -1,
    		'meta_query' => array(
    		  'course-name' => 'open-water-diver',
    		  'compare' => 'like'
    		),
    	);
    	$query = new WP_Query( $args );
    
    	if ( $query->have_posts() ) :
    		while ( $query->have_posts() ) : $query->the_post();
    			return '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
    		endwhile;
    	endif;
    }
    add_shortcode( 'owurl', 'ow_url_func' );
    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    A custom attribute is stored as postmeta under the key of _product_attributes. However, the name and terms are in a serialized array so this will be a bit more complicated than your query above.

    I can’t really help with custom coded solutions here though, so you may need to hire a developer if you can’t quite figure out the code you need:

    http://jobs.wordpress.net/
    https://codeable.io/
    https://woocommerce.com/experts/?project-scope%5B0%5D=1819

    Hi,

    I think what you are looking for is this snippet – http://snippet.fm/snippets/query-woocommerce-products-product-specific-custom-attribute/.

    Best regards,
    Tom

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Query products by custom product attribute’ is closed to new replies.