Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #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?

    #1412022

    Hey 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,
    Ismael

    #1412068

    Brilliant, thanks – works a treat!

    Out of interest – what’s the reason why
    if ($query['category_name'] = 'events';)
    doesn’t work?

    #1412128

    Hi,

    Glad to know that the changes are working. The reason why the above condition is not working is that ‘category_name’ is not a valid parameter or it does not exist in the $query.

    Let us know if you have more questions.

    Best regards,
    Ismael

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.