-
AuthorPosts
-
June 28, 2023 at 7:39 pm #1411991
I have used this code
add_filter('avia_masonry_entries_query', 'avia_masonry_custom_query'); function avia_masonry_custom_query( $query ) { $query['meta_key'] = 'display_order'; $query['orderby'] = 'meta_value_num'; $query['order'] = 'ASC'; return $query; }
which works as I want, based on a custom field value of display_order added to posts with the category ‘events’. No other posts have the custom field.
The problem is I have 3 pages with a Masonry element. Each page shows just a single category: the slugs are events, news, initiatives. However I only want the sort function to work on the Events page. With the above code, the News and Initiatives pages are now not showing any posts (because they don’t have the custom field).
I’ve tried adding
if ($query['category_name'] = 'events';)
to the above as the second line in the function (plus more curly brackets) but it doesn’t work. I don’t know PHP well, as you can probably tell…Is it possible to use the category and just have the function work on the Events page only, with the other two pages sorting by creation date? Any suggestions?
June 29, 2023 at 7:35 am #1412022Hey zimbo,
Thank you for the inquiry.
You can wrap the query changes within a conditional function like the is_page function. The following code, for instance, will modify the query only when the page’s ID is 123.
add_filter('avia_masonry_entries_query', 'avia_masonry_custom_query'); function avia_masonry_custom_query( $query ) { if( is_page(123) ) { $query['meta_key'] = 'display_order'; $query['orderby'] = 'meta_value_num'; $query['order'] = 'ASC'; } return $query; }
Make sure to replace the is_page value with the actual ID of the Events page. You can also use an array of page IDs in the is_page function. For more details about the conditional function, please check the documentation below.
// https://developer.wordpress.org/reference/functions/is_page/
Best regards,
IsmaelJune 29, 2023 at 2:13 pm #1412068Brilliant, thanks – works a treat!
Out of interest – what’s the reason why
if ($query['category_name'] = 'events';)
doesn’t work?June 30, 2023 at 4:52 am #1412128 -
AuthorPosts
- You must be logged in to reply to this topic.