Posts 2 posts not functioning - php

I am using the Posts to Post plugin for Wordpress (version: 1.6.3) to link my posts with a custom post type I created. This custom post type consists of 'authors' that can be linked to a post. Yesterday it was functioning as it should but as for today, the overview is generating a list of 'posts' instead of a list of 'authors'.
I am using the following function to generate a list of authors.
function my_connection_types() {
p2p_register_connection_type( array(
'name' => 'posts_to_auteurs',
'from' => 'post',
'to' => 'auteur'
) );
}
add_action( 'p2p_init', 'my_connection_types' );
Does anyone know what's wrong? I'm dying for help...

Related

Adding post fields to post data in custom WordPress REST API endpoint

I have a custom field that I have added to every wordpress post. I created an api endpoint that queries posts where the custom field is a certain value. That works great, the code is:
function fp_acf() {
$args = array(
'meta_key' => 'featured_post',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
return $the_query;
}
add_action('rest_api_init', function() {
register_rest_route('wp/v2', 'featured_posts', array(
'methods' => array('GET', 'POST'),
'callback' => function() {
return fp_acf();
},
));
});
The Problem:
The data that is returned from that endpoint doesn't contain all of the post data that is typically included in, say "/wp/v2/posts", specifically the slug or link.
Question:
I am wondering if it is possible to add the slug of the posts returned in this question query endpoint to the post data being returned?
If I understood the problem correctly and under the presumption that you are using the ACF plugin, going by the function name fp_acf, you will want to register a custom REST field that contains the value of your desired ACF field.
To get this done you have to do the following:
First, make sure all of your posts that the WP_Query is going over have the ability to be shown in the REST api.
Default posts and pages already show in the REST api but for any custom post types you will need to add 'show_in_rest' => true to their register_post_type argument array.
Next, you want to actually register the custom REST field, you do this in your rest_api_init callback like so:
add_action('rest_api_init', function() {
// ...
register_rest_field( array( 'your_post_type' ), 'is_featured_post', array(
'get_callback' => function() {
return get_field('featured_post');
},
'schema' => array(
'description' => 'Shows whether the post is featured',
'type' => 'string'
),
));
});
Obviously, you can change the return value of the get_callback function to whatever you need. You should be able to use all of the same functions, such as
get_the_permalink, get_the_title etc. as you would when looping over posts in a template.
N.B. you say you need to get the slug of the posts, you should find that there already is a slug property that is returned by the REST api for posts and pages.

How to create a custom order to display posts and taxonomies

i'm a music producer and new to coding. I'm working on my website and i have created a custom taxonomy called "genre" which is linked to a custom post type called "my-music". below is the code i used for the options when adding genres to display on a page. please see the attached image for reference. athis image (https://drive.google.com/file/d/1liovGGOnjgx-IxjMklk1ZVNyyCaBpqXF/view?usp=sharing).
<?php if (!defined('FW')) die('Forbidden');
`$options = array(
'genre_heading' => array(
'label' => __('Title', 'miraculous'),
'type' => 'text'
),
'number_limits' => array(
'label' => __('Number of genre', 'miraculous'),
'type' => 'text'
),
);
The above code and attached image then display the created values under the genre taxonomy. please this image for reference. ( https://drive.google.com/file/d/191ArcB0y1sh9vuKynkknHrhP7GOCJ7Sv/view?usp=sharing ). by Default it display the genres by alphabetical order, i would like to add an option to the above codes called display order which should include 3 select options 1. Most Played 2.Alphabetical Order 3.Top Rated.
The "my-music' custom post type already has a function called (miraculous_song_view_counter) which counts how many times a song has been played. this is the code i used to create it ( https://drive.google.com/file/d/1kbcHW476CvFH5xIsjsXT0PIDtUIgLOVE/view?usp=sharing ). and i'm using Unyson feedback extension for rating posts. Is there a way to use the data collected from this rating submissions and music count function to create those display order options?

WordPress api returns distinct data

I am developing a restful api for wordpress blog. In a url I'm returning the details of categories of that blog. The same api returns array in one blog and object in another.
The code is-
function categories(){
$categories = get_categories(array(
'orderby' => 'name',
'order' => 'ASC'
));
return $categories;
}
add_action( 'rest_api_init', function ( $server ) {
$server->register_route( 'categories', '/categories', array(
'methods' => 'GET',
'callback' => 'categories',
));
});
Here is the output-
Blog 1
Blog 2
I need same type of data to be returned, so that I can further process that data.
Advise you to please check you categories result set for both the blogs, you will be able to find the difference, In blog 1 result set something might be missing when you are preparing array for json_encode. I use to face a lot of problems like this when I started developing REST API.
If problem still exist then please post both resultsets, so that we can identify.

Using wp_dropdown_page() to display a dropdown of a custom post type

I'm trying to create a dropdown list with the list of a certain post type.
I saw that I could use the wp_dropdown_pages function to do that.
I added this code inside my td but when the page displays the code is not interpreted. The are only few examples to look at.
<td><?php wp_dropdown_pages(array('id'=>'marque0','post_type' => 'Brands')); ?></td>
additionally I need to filter a second dropdown list which contains only child post of the parent selected in the first dropdown. To do that I need the id of the parent post. How can I get this from the element selected in the first dropdown list ?
Thanks a lot.
Update :
I found a way to go around the usage of PHP inside the wordpress page, I created shortcodes to call the wp_dropdown_pages, then I use the shortcodes inside my page.
Surprisingly enough (!), just specify in your custom post type's settings 'hierarchical' => true, not the hierarchical argument in wp_dropdown_pages() function.
register_post_type( 'brands', array(
'labels' => array(
..........
),
.........
'hierarchical' => true,
.........
));
Then it should work:
wp_dropdown_pages(array(
'id' => 'marque0',
..........
'post_type' => 'brands'
));

WPAlchemy Meta Box: Place meta box on specific post / page... conflict with "types" argument

I'm using the WPAlchemy class to create a metabox. I want to place this metabox in a number of post editors in the backend.
Currently it's working just fine with the following code:
$video_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_videoMeta',
'title' => 'Videos',
'types' => array('characters','homepage'),
'template' => THEMEASSETS . '/functions/video_meta.php'
));
What I want to do though is additionally place the metabox on the post editor for post ID #22. Supposedly the following code should work:
$video_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_videoMeta',
'title' => 'Videos',
'types' => array('characters','homepage'),
'template' => THEMEASSETS . '/functions/video_meta.php',
'include_post_id' => 22
));
But it doesn't work unless I add in 'page' into the array of post types, which adds the metabox to all pages (not just post ID 22).
Is there a way to use poth the types and the include post ID arguments?
I had the same issue. Acutally, I had set a metabox to two custom post types and wanted it to be displayed on a specific page.
$video_metabox = new WPAlchemy_MetaBox(array(
'id' => '_videoMeta',
'title' => 'Videos',
'types' => array('characters','homepage', 'page'),
'template' => THEMEASSETS . '/functions/video_meta.php',
'include_post_id' => 22
));
Just add the bult in 'page' post type and everything should work fine. It did for me.

Categories