/var/log

Tag: WordPress

Laravel and WordPress combined

WordPress and Laravel are popular frameworks that are good at what they are designed for. WordPress excels in managing content with revisions, taxonomies and a pretty good UI. Laravel is good in managing structured data with its eloquent models, routing and securing access to routes. Sometimes a project requires both the management of a lot […]

Execute command line PHP script from within WordPress

You can try to load the wp-load.php of wp-config.php files manually, but then some defines are not set (like HTTP_HOST). An easier way is to use the eval-file method of the wp-cli package. No you can just do: And in your file, you can call all the usual WordPress methods. Only make sure to explicitly […]

Move WordPress Multisite to new URLS

Here are the steps involved when moving a WordPress Multisite installation to a new domain name. In my case I wanted to move everything (code + database) from development to production. After copying everything to the new environment, you have to change the following items: Make sure to add or update your wp-config.php file: define('DOMAIN_CURRENT_SITE', […]

WordPress Users, Roles and Capabilities

I find the capabilities one of the most difficult to grasp concepts in WordPress development. On one hand, you have the capabilities of a custom post type and on the other hand you have the capabilities of a user or user role. And then you have the 2 other capabilities options you can set when registering a custom […]

Get custom post type by custom taxonomy on the WordPress rest api

To get custom post types and filter them by custom taxonomies, you can do the following: or filter by more than one taxonomy (this means: give me posts from taxonomy 1 OR 3 OR 6): or to exclude posts with specific taxonomies: Maybe good to know: combining categories and tags means AND, so for example […]

Custom taxonomies vs post meta data

There are many articles about the difference between custom taxonomies and post meta data. Here are some differences I experienced: There is no index on the post meta value. With the builtin WP_Query functionality, you cannot search with operators like <, >, =, >=, BETWEEN, etc. in custom taxonomies, so if your values are numbers, […]

URL structure of WordPress custom post types and taxonomies

Explanation about register_taxonomy and register_post_type and how this is reflected in the URL structure. register_taxonomy( 'my_newscat', ['post'], [ 'labels' => ['name' => 'News category'], 'public' => true, 'hierarchical' => true, 'show_admin_column' => true, 'rewrite' => [ 'slug' => 'newscategories' ], 'query_var' => 'newscategories' ] ); If we didn't specify a rewrite slug, we had to […]

Show custom post types in category archives

Custom post types are not displayed in the category and tag archives by default, even if you indicated in the register_post_type call that it should support these taxonomies. The way to include them in the category and tag archives is to use the pre_get_posts action: add_action('pre_get_posts', 'mycode_pre_get_posts'); function mycode_pre_get_posts($query) { if ((is_home() || is_category() || […]

Single selected taxonomy in WordPress

The category taxonomy is by default a multiselect widget that uses checkboxes. There are some ways to only allow one selection, like implementing your own Walker_Category_Checklist. But the most simple solution is just replace the checkbox inputs with radio input boxes with some Javascript. In the example below, I have a taxonomy "level" that I want […]