Git Product home page Git Product logo

wp-primary-category's Introduction

WordPress Primary Category

Select the primary category

https://wordpress.org/plugins/wp-primary-category

Installation

  1. Copy the wp-primary-category folder into your wp-content/plugins folder
  2. Activate the WordPress Primary Category plugin via the plugin admin page

Settings

How to able the primary category feature?

  1. Open the settings page ( To open that page, we have to two alternatives );
  2. Select the taxonomies what you want to able the feature;
  3. After selected all taxonomies, click in the save button.

First Alternative: Settings > Primary Category

first alternative

Second Alternative: Plugins admin page > Settings link in WordPress Primary Category

second alternative

Settings Page

settings page

Selecting

After edit your settings, now you can select which will be your primaries categories.

settings page

Functions

Name Argument(s)
get_primary_categories mixed ( int, WP_Post, NULL ) $post
required
--
string $output
default value: OBJECT
others values: OBJECT, ARRAY_A, ARRAY_N or "ID"
optional
get_primary_category mixed ( int, WP_Term, object, string ) $taxonomy
required
--
mixed ( int, WP_Post, NULL ) $post
default value: NULL
optional
--
string $output
default value: OBJECT
others values: OBJECT, ARRAY_A, ARRAY_N or "ID"
optional
the_primary_category mixed ( int, WP_Term, object, string ) $taxonomy
required
--
mixed ( int, WP_Post, NULL ) $post
default value: NULL
optional
--
string $output
default value: "link"
others values: "name"
optional--
string $echo
default value: true
optional
is_primary_category mixed ( int, WP_Term, object ) $term
required
--
mixed ( string, NULL ) $taxonomy
default value: NULL
optional
--
mixed ( int, WP_Post, NULL ) $post
default value: NULL
optional
has_primary_category mixed ( int, WP_Post, NULL ) $post
default value: NULL
optional

Basic example

$args = array(
	'post_type' => 'book',
);

$query = new WP_Query( $args );
if ( $query->have_posts() ) {
	echo '<ul>';
	while ( $query->have_posts() ) {
		$query->the_post();
		echo '<li>' . get_the_title();
		if ( has_primary_category() ) {
			echo ' - ';
			the_primary_category( 'genre' );
		}
		echo '</li>';
	}
	echo '</ul>';
	wp_reset_postdata();
}

Filters

Filter Argument(s)
wp_primary_category_update_option array $data
mixed ( array, boolean, NULL ) $option
wp_primary_category_option array $data
wp_primary_category_not_allowed_post_types array $post_types
wp_primary_category_post_types_args array $args
wp_primary_category_post_types mixed ( array, boolean ) $post_types
wp_primary_category_not_allowed_taxonomies array $taxonomies
wp_primary_category_taxonomies_args array $args
wp_primary_category_taxonomies mixed ( array, boolean ) $taxonomies
wp_primary_category_html string $html
mixed ( int, WP_Term, object, string ) $taxonomy
mixed ( int, WP_Post, NULL ) $post
string $output
wp_primary_category_rest_api_output string ( OBJECT, ARRAY_A, ARRAY_N or "ID" ) $output
array $request
wp_primary_category_rest_api_data array $data
array $request

How to use the filters?

See bellow how to exclude page post type.

add_filter( 'wp_primary_category_post_types', function( $post_types ) {
	if ( isset( $post_types['page'] ) ) {
		unset( $post_types['page'] );
	}

	return $post_types;
} );

or

add_filter( 'wp_primary_category_not_allowed_post_types', function( $post_types ) {
	$post_types['page'] = 'page';

	return $post_types;
} );

Shortcode

Tag Attribute(s)
the_primary_category mixed ( int, WP_Term, object, string ) $taxonomy
required
--
mixed ( int, WP_Post, NULL ) $post
default value: NULL
optional
--
string $output
default value: "link"
others values: "name"
optional

How to use

[wp_primary_category taxonomy="genre"]
[wp_primary_category taxonomy="genre" post="1"]
[wp_primary_category taxonomy="genre" post="1" output="name"]

WP_Query

See below how get posts with genre primary category

$args = array(
	'post_type' => 'book',
	'meta_query' => array(
		array(
			'key'     => '_wp_primary_category_genre',
			'value'   => array( 2, 4 ), // terms id
			'compare' => 'IN',
		)
	),
);

$query = new WP_Query( $args );
if ( $query->have_posts() ) {
	while ( $query->have_posts() ) {
		$query->the_post();
		// your code here
	}
}

https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

REST API

Endpoint: /wp-json/wp/v2/posts/1

Default answer:

{
	"id": 1,
	"title": {
		"rendered": "Hello world!"
	},
	...
	"primary_categories": {
		"category": 1,
		"genre": 2
	},
	...
}

Use the filter below to change the answer:

add_filter( 'wp_primary_category_rest_api_data', function( $data ) {
	if ( isset( $data['genre'] ) ) {
		unset( $data['genre'] );
	}

	return $data;
} );

The new answer:

{
	"id": 1,
	"title": {
		"rendered": "Hello world!"
	},
	...
	"primary_categories": {
		"category": 1
	},
	...
}

Use the filter below to change the output:

add_filter( 'wp_primary_category_rest_api_output', function() {
	return OBJECT; // OBJECT, ARRAY_A, ARRAY_N or 'ID'
} );

The new answer:

{
	"id": 1,
	"title": {
		"rendered": "Hello world!"
	},
	...
	"primary_categories": {
		"category": {
			"term_id": 1,
			"name": "My category",
			"slug": "my-category",
			"term_group": 0,
			"term_taxonomy_id": 2,
			"taxonomy": "category",
			"description": "",
			"parent": 0,
			"count": 1,
			"filter": "raw"
		}
	},
	...
}

To Do

  • add support to Gutenberg ( block editor )

wp-primary-category's People

Contributors

airesvsg avatar

Watchers

 avatar  avatar

Forkers

mirsch

wp-primary-category's Issues

impossible to remove primary category once it has been set

If you set a primary category and update the post it's impossible to remove the primary category and have a post without a primary category again.

Steps to reproduce:

  1. Set a primary category
  2. click "Update"
  3. uncheck the selected primary category
  4. click "Update"
  5. the category is selected again

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.