• Resolved AndreasJa

    (@andreasja)


    Hello!

    First, thank you for an awesome plugin. I am using it to display different kind of “product cards” on my upcoming affiliate product review blog.

    I’m wondering if it is possible to Order by a Custom field?

    I have a list layout where each post has a custom field called “list_number” in which I assign a number, #1, #2, #3, #4 etc. I would like to order the posts by this number.

    I am not good in explaining, but hope you understand.

    I understood from a different support topic that I can do this by using your filter “query_args”. But I am not sure how to use it :/

    Thank you for any help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Code Amp

    (@codeamp)

    Hey @andreasja

    You would need to use our custom-layouts/layout/query_args filter from here:
    https://customlayouts.com/documentation/action-filter-reference/

    And then add this (modified code) to your child themes functions.php file.

    Try this:

    
    function layout_query_args( $query_args ) {
    	// Modify the query args
    	$query_args[ 'meta_key' ] = 'your_key';
    	$query_args[ 'orderby' ] = 'meta_value_num';
    	$query_args[ 'order' ] = 'DESC';
    	
    	// Always return in WP filters
    	return $query_args;
    }
    
    add_filter( 'custom-layouts/layout/query_args', 'layout_query_args', 10 );
    

    You would need to change your_key to your custom field key (if you are using ACF, let me know, as its not as straight forward as you might think)

    Also: just to warn you, this code will apply to all layouts on your site.

    What you probably want to do is restrict this to a layout ID or specific page.

    How are you creating your layouts, as blocks, or via our admin pages?

    Thanks

    • This reply was modified 3 years, 1 month ago by Code Amp.
    • This reply was modified 3 years, 1 month ago by Code Amp.
    • This reply was modified 3 years, 1 month ago by Code Amp.
    Thread Starter AndreasJa

    (@andreasja)

    Hello!

    Thank you for the reply and help.

    I use ACF and I create the layouts via admin pages and insert shortcodes.

    I tried it but as you said it applied to all my layouts and the one that do not have the custom field “list_number” did not show any content at all.

    —–
    Will try to explain how I use it, it’s on localhost right now so can’t show you.

    I have three custom post types called Products, Gifts, and Toplists. These are for products (a little bit like thisiswhyimbroke.com) and I use your plugin to display the products with different custom fields. (I will buy your search and filter plugin too soon to use on some places on the website)

    But I also use your plugin to display regual post grids.

    The “Products”, “Gifts” and “Toplists” custom post type grids I would like to orderby the custom field “list_number”, #1,#2,#3 etc.

    But the regual grids with posts I want to order by Published Date. If this is possible.

    I tried

    function layout_query_args( $query_args ) {
    	// Modify the query args
    
            $query_args['post_type'] = array( 'products', 'gifts', 'toplists' );
    	$query_args[ 'meta_key' ] = 'list_number';
    	$query_args[ 'orderby' ] = 'meta_value_num';
    	$query_args[ 'order' ] = 'DESC';
    
    	// Always return in WP filters
    	return $query_args;
    }
    
    add_filter( 'custom-layouts/layout/query_args', 'layout_query_args', 10 );

    and

    function layout_query_args( $query_args ) {
    	// Modify the query args
    
    global $post;
    if ( $post->post_type == 'products', 'gifts', 'toplists' ) {
    
    	$query_args[ 'meta_key' ] = 'list_number';
    	$query_args[ 'orderby' ] = 'meta_value_num';
    	$query_args[ 'order' ] = 'DESC';
    
    }
    
    	// Always return in WP filters
    	return $query_args;
    }
    
    add_filter( 'custom-layouts/layout/query_args', 'layout_query_args', 10 );

    But that did not work.

    Hope you understood my long text, and thank you again for the help!

    Thread Starter AndreasJa

    (@andreasja)

    So, I came up with a workaround by adding the custom field “list_number” to regual Posts as well and gave the field a Default Value “0” in ACF, and then used $query_args[ 'orderby' ] = 'meta_value_num date'; to order by date when all the posts have the same number.

    Now the custom post types Products, Gifts, Toplists will order by the numbers I give them, and posts will order by date.

    function layout_query_args( $query_args ) {
    	// Modify the query args
    
    	$query_args[ 'meta_key' ] = 'list_number';
    	$query_args[ 'orderby' ] = 'meta_value_num  date';
    	$query_args[ 'order' ] = 'DESC';
    
    	// Always return in WP filters
    	return $query_args;
    }
    
    add_filter( 'custom-layouts/layout/query_args', 'layout_query_args', 10 );

    But, is there another better way to do this or should I stick with this solution? Thanks!

    Plugin Author Code Amp

    (@codeamp)

    Hey @andreasja

    Yeah I think this is the right approach.

    A WordPress query, if ordered by a custom field will remove any entries that don’t have that custom field associated – which you’ve already discovered and worked around by adding this custom field to the “post” post type.

    That’s the best way around it, but it is a work around, I wish there was a way to specific in a query whether or not we wanted to include results which don’t have the custom field but as far as I’m aware it’s not possible yet…

    There was one point I mentioned earlier regarding conditionally loading this query modification.

    If you are creating a specific page, with your layouts on, then you can simply add a check to make sure we are on the right page, before adding your logic:

    function layout_query_args( $query_args, $layout_id ) {
    	// Block editor way would be to check what the current page is
    	/* if ( ! is_page( 'my-page-name' ) ) {
    		return $query_args;
    	} */
    	
    	// Because you created layouts in our admin pages, they have an ID so you can test for it here
    	// Change 123 for your own layout ID
    	if ( $layout_id	!== 123 ) {
    		// If the ID doesn't match, get out of here
    		return $query_args;
    	}
    	
    	// Now we know the modifications will only apply to a layout with ID - 123
    	// We can add our modifications
    	$query_args[ 'meta_key' ] = 'list_number';
    	$query_args[ 'orderby' ] = 'meta_value_num  date';
    	$query_args[ 'order' ] = 'DESC';
    
    	// Always return in WP filters
    	return $query_args;
    }
    
    add_filter( 'custom-layouts/layout/query_args', 'layout_query_args', 10, 2 );

    Here you can see, I’ve added $layout_id to the function, and tested against layout ID 123 – change this for your layout ID.

    Also note, the add_filter(... line has a , 2 argument added, which allows us to get the $layout_id passed into the function.

    Let me know if that makes sense

    Best
    – Ross

    • This reply was modified 3 years, 1 month ago by Code Amp.
    • This reply was modified 3 years, 1 month ago by Code Amp.
    • This reply was modified 3 years, 1 month ago by Code Amp.
    Thread Starter AndreasJa

    (@andreasja)

    Many thanks for the help Ross! This works great and I will mark as resolved, thanks again.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Order by Custom Field’ is closed to new replies.